Module: Auth0::Api::V2::Rules

Included in:
Auth0::Api::V2
Defined in:
lib/auth0/api/v2/rules.rb

Overview

Methods to use the rules endpoints

Instance Method Summary collapse

Instance Method Details

#create_rule(name, script, order = nil, enabled = true, stage = 'login_success') ⇒ json

Creates a new rule according to the JSON object received in body. executes first. If no order is provided it will automatically be one greater than the current maximum. ‘user_registration’ or ‘user_blocked’.

Parameters:

  • name (string)

    The name of the rule. Can only contain alphanumeric characters, spaces and ‘-’.

  • script (string)

    A script that contains the rule’s code.

  • order (integer) (defaults to: nil)

    The rule’s order in relation to other rules. A rule with a lower order than another rule

  • enabled (string) (defaults to: true)

    True if the rule is enabled, false otherwise.

  • stage (string) (defaults to: 'login_success')

    The rule’s execution stage ‘login_success’ or ‘login_failure’ or ‘pre_authorize’ or

Returns:

  • (json)

    Returns the created rule.

Raises:

See Also:



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/auth0/api/v2/rules.rb', line 65

def create_rule(name, script, order = nil, enabled = true, stage = 'login_success')
  raise Auth0::InvalidParameter, 'Must supply a valid name' if name.to_s.empty?
  raise Auth0::InvalidParameter, 'Must supply a valid script' if script.to_s.empty?
  request_params = {
    name: name,
    enabled: enabled,
    script: script,
    order: order,
    stage: stage
  }
  post(rules_path, request_params)
end

#delete_rule(rule_id) ⇒ Object

Deletes a rule.

Parameters:

  • rule_id (string)

    The id of the rule to delete.

Raises:

See Also:



94
95
96
97
98
# File 'lib/auth0/api/v2/rules.rb', line 94

def delete_rule(rule_id)
  raise Auth0::InvalidParameter, 'Must supply a valid rule id' if rule_id.to_s.empty?
  path = "#{rules_path}/#{rule_id}"
  delete(path)
end

#rule(rule_id, fields: nil, include_fields: nil) ⇒ json Also known as: get_rule

Retrieves a rule by its ID. Accepts a list of fields to include or exclude in the result.

Parameters:

  • rule_id (string)

    The id of the rule to retrieve.

  • fields (string) (defaults to: nil)

    A comma separated list of fields to include or exclude from the result.

  • include_fields (boolean) (defaults to: nil)

    True if the fields specified are to be included in the result, false otherwise.

Returns:

  • (json)

    Returns the rule.

Raises:

See Also:



42
43
44
45
46
47
48
49
50
# File 'lib/auth0/api/v2/rules.rb', line 42

def rule(rule_id, fields: nil, include_fields: nil)
  raise Auth0::InvalidParameter, 'Must supply a valid rule id' if rule_id.to_s.empty?
  path = "#{rules_path}/#{rule_id}"
  request_params = {
    fields:   fields,
    include_fields:   include_fields
  }
  get(path, request_params)
end

#rules(enabled: nil, fields: nil, include_fields: nil, stage: nil, page: nil, per_page: nil) ⇒ json Also known as: get_rules

Retrieves a list of all rules. Accepts a list of fields to include or exclude. The enabled parameter can be specified to get enabled or disabled rules. The rule’s stage of executing could be set to the following values login_success, login_failure or pre_authorize

Parameters:

  • enabled (boolean) (defaults to: nil)

    If provided retrieves rules that match the value, otherwise all rules are retrieved.

  • fields (string) (defaults to: nil)

    A comma separated list of fields to include or exclude from the result.

  • include_fields (boolean) (defaults to: nil)

    True if the fields specified are to be included in the result, false otherwise.

  • stage (string) (defaults to: nil)

    Retrieves rules that match the execution stage (defaults to login_success).

  • page (int) (defaults to: nil)

    Page number to get, 0-based.

  • per_page (int) (defaults to: nil)

    Results per page if also passing a page number.

Returns:

  • (json)

    Returns the existing rules.

See Also:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/auth0/api/v2/rules.rb', line 21

def rules(enabled: nil, fields: nil, include_fields: nil, stage: nil, page: nil, per_page: nil)
  request_params = {
    enabled:          enabled,
    fields:           fields,
    include_fields:   include_fields,
    stage:            stage,
    page:             page,
    per_page:         per_page
  }
  get(rules_path, request_params)
end

#update_rule(rule_id, fields_to_update = {}) ⇒ json

Updates a rule.

Parameters:

  • rule_id (string)

    The id of the rule to retrieve.

  • fields_to_update (hash) (defaults to: {})

    The Hash fields_to_update used to define the rule’s properties.

Returns:

  • (json)

    Returns the updated rule.

Raises:

See Also:



84
85
86
87
88
89
# File 'lib/auth0/api/v2/rules.rb', line 84

def update_rule(rule_id, fields_to_update = {})
  raise Auth0::InvalidParameter, 'Must supply a valid rule id' if rule_id.to_s.empty?

  path = "#{rules_path}/#{rule_id}"
  patch(path, fields_to_update)
end