Class: Warrant::Role

Inherits:
Object show all
Includes:
WarrantObject
Defined in:
lib/warrant/models/role.rb

Constant Summary collapse

OBJECT_TYPE =
"role"

Instance Attribute Summary

Attributes inherited from Object

#created_at, #meta, #object_id, #object_type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

batch_create, batch_delete

Class Method Details

.assign_to_user(user_id, role_id, relation: "member", options: {}) ⇒ Warrant

Assign a role to a user

Parameters:

  • user_id (String)

    The user_id of the user you want to assign a role to.

  • role_id (String)

    The role_id of the role you want to assign to a user.

  • relation (String) (defaults to: "member")

    The relation for this role to user association. The relation must be valid as per the role object type definition.

Returns:

  • (Warrant)

    warrant assigning role to user

Raises:



170
171
172
# File 'lib/warrant/models/role.rb', line 170

def self.assign_to_user(user_id, role_id, relation: "member", options: {})
    Warrant.create({ object_type: Role::OBJECT_TYPE, object_id: role_id }, relation, { object_type: User::OBJECT_TYPE, object_id: user_id }, nil, options)
end

.create(params = {}, options = {}) ⇒ Role

Creates a role with the given parameters

Examples:

Create a new Role with the role id “test-role”

Warrant::Role.create(role_id: "test-role")

Parameters:

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :role_id (String)

    User defined string identifier for this role. If not provided, Warrant will create an id for the role and return it. In this case, you should store the id in your system as you will need to provide it for any authorization requests for that role. Note that roleIds in Warrant must be composed of alphanumeric chars, ‘-’, and/or ‘_’. (optional)

  • :meta (Hash)

    A JSON object containing additional information about this role (e.g. name/description) to be persisted to Warrant. (optional)

Returns:

  • (Role)

    created role

Raises:



30
31
32
33
# File 'lib/warrant/models/role.rb', line 30

def self.create(params = {}, options = {})
    object = Object.create({ object_type: OBJECT_TYPE, object_id: params[:role_id], meta: params[:meta] }, options)
    return Role.new(object.object_id, object.meta, object.created_at)
end

.delete(role_id, options = {}) ⇒ nil

Deletes a role with given role id

Examples:

Delete a Role with the role id “test-role”

Warrant::Role.delete("test-role")

Parameters:

  • role_id (String)

    The role_id of the role to delete.

Returns:

  • (nil)

    if delete was successful

Raises:



48
49
50
# File 'lib/warrant/models/role.rb', line 48

def self.delete(role_id, options = {})
    return Object.delete(OBJECT_TYPE, role_id, options)
end

.get(role_id, options = {}) ⇒ Role

Get a role with the given role_id

Parameters:

  • role_id (String)

    The role_id of the role to retrieve.

Returns:

  • (Role)

    retrieved role

Raises:



88
89
90
91
# File 'lib/warrant/models/role.rb', line 88

def self.get(role_id, options = {})
    object = Object.get(OBJECT_TYPE, role_id, options)
    return Role.new(object.object_id, object.meta, object.created_at)
end

.list(filters = {}, options = {}) ⇒ Array<Role>

Lists all roles for your organization

Examples:

List all roles

Warrant::Role.list()

Parameters:

  • filters (Hash) (defaults to: {})

    Filters to apply to result set

  • options (Hash) (defaults to: {})

    Options to apply on a per-request basis

Options Hash (filters):

  • :limit (Integer)

    A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)

  • :prev_cursor (String)

    A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)

  • :next_cursor (String)

    A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)

  • :sort_by (String)

    The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are object_type, object_id, and created_at (optional)

  • :sort_order (String)

    The order in which to sort the result by. Valid values are ASC and DESC. Defaults to ASC. (optional)

Options Hash (options):

  • :warrant_token (String)

    A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)

Returns:

  • (Array<Role>)

    all roles for your organization

Raises:



71
72
73
74
75
76
# File 'lib/warrant/models/role.rb', line 71

def self.list(filters = {}, options = {})
    filters.merge({ object_type: "role" })
    list_response = Object.list(filters, options)
    roles = list_response.results.map{ |object| Role.new(object.object_id, object.meta, object.created_at)}
    return ListResponse.new(roles, list_response.prev_cursor, list_response.next_cursor)
end

.list_for_user(user_id, filters = {}, options = {}) ⇒ Array<Role>

List roles for user

Parameters:

  • user_id (String)

    The user_id of the user you want to retrieve roles for.

  • filters (Hash) (defaults to: {})

    Filters to apply to result set

  • options (Hash) (defaults to: {})

    Options to apply on a per-request basis

Options Hash (filters):

  • :object_type (String)

    Only return objects with an ‘objectType` matching this value

  • :limit (Integer)

    A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)

  • :prev_cursor (String)

    A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)

  • :next_cursor (String)

    A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)

  • :sort_by (String)

    The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are object_type, object_id, and created_at (optional)

  • :sort_order (String)

    The order in which to sort the result by. Valid values are ASC and DESC. Defaults to ASC. (optional)

Options Hash (options):

  • :warrant_token (String)

    A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)

Returns:

  • (Array<Role>)

    all assigned roles for the user

Raises:



150
151
152
153
154
# File 'lib/warrant/models/role.rb', line 150

def self.list_for_user(user_id, filters = {}, options = {})
    query_response = Warrant.query("select role where user:#{user_id} is *", filters: filters, options: options)
    roles = query_response.results.map{ |result| Role.new(result.object_id, result.meta) }
    return ListResponse.new(roles, query_response.prev_cursor, query_response.next_cursor)
end

.remove_from_user(user_id, role_id, relation: "member", options: {}) ⇒ nil

Remove a role from a user

Parameters:

  • user_id (String)

    The user_id of the role you want to remove a role from.

  • role_id (String)

    The role_id of the role you want to remove from a user.

  • relation (String) (defaults to: "member")

    The relation for this role to user association. The relation must be valid as per the role object type definition.

Returns:

  • (nil)

    if remove was successful

Raises:



188
189
190
# File 'lib/warrant/models/role.rb', line 188

def self.remove_from_user(user_id, role_id, relation: "member", options: {})
    Warrant.delete({ object_type: Role::OBJECT_TYPE, object_id: role_id }, relation, { object_type: User::OBJECT_TYPE, object_id: user_id }, nil, options)
end

.update(role_id, meta, options = {}) ⇒ Role

Updates a role with the given role_id and params

Examples:

Update role “test-role”‘s name

Warrant::Role.update("test-role", { name: "Test Role" })

Parameters:

  • role_id (String)

    The role_id of the role to be updated.

  • meta (Hash)

    A JSON object containing additional information about this role (e.g. name/description, etc.) to be persisted to Warrant.

Returns:

  • (Role)

    updated role

Raises:



108
109
110
111
# File 'lib/warrant/models/role.rb', line 108

def self.update(role_id, meta, options = {})
    object = Object.update(OBJECT_TYPE, role_id, meta, options)
    return Role.new(object.object_id, object.meta, object.created_at)
end

Instance Method Details

#assign_permission(permission_id, relation: "member", options: {}) ⇒ Permission

Assign a permission to a role

Parameters:

  • permission_id (String)

    The permission_id of the permission you want to assign to the role.

  • relation (String) (defaults to: "member")

    The relation for this permission to user association. The relation must be valid as per the permission object type definition.

Returns:

Raises:



226
227
228
# File 'lib/warrant/models/role.rb', line 226

def assign_permission(permission_id, relation: "member", options: {})
    return Permission.assign_to_role(role_id, permission_id, relation: relation, options: options)
end

#list_permissions(filters = {}, options = {}) ⇒ Permission

List assigned permissions for the role

Parameters:

  • filters (Hash) (defaults to: {})

    Filters to apply to result set

  • options (Hash) (defaults to: {})

    Options to apply on a per-request basis

Options Hash (filters):

  • :object_type (String)

    Only return objects with an ‘objectType` matching this value

  • :limit (Integer)

    A positive integer representing the maximum number of items to return in the response. Must be less than or equal to 1000. Defaults to 25. (optional)

  • :prev_cursor (String)

    A cursor representing your place in a list of results. Requests containing prev_cursor will return the results immediately preceding the cursor. (optional)

  • :next_cursor (String)

    A cursor representing your place in a list of results. Requests containing next_cursor will return the results immediately following the cursor. (optional)

  • :sort_by (String)

    The column to sort the result by. Unless otherwise specified, all list endpoints are sorted by their unique identifier by default. Supported values for objects are object_type, object_id, and created_at (optional)

  • :sort_order (String)

    The order in which to sort the result by. Valid values are ASC and DESC. Defaults to ASC. (optional)

Options Hash (options):

  • :warrant_token (String)

    A valid warrant token from a previous write operation or latest. Used to specify desired consistency for this read operation. (optional)

Returns:

Raises:



209
210
211
# File 'lib/warrant/models/role.rb', line 209

def list_permissions(filters = {}, options = {})
    return Permission.list_for_role(role_id, filters, options)
end

#remove_permission(permission_id, relation: "member", options: {}) ⇒ nil

Remove a permission from a role

Parameters:

  • permission_id (String)

    The permission_id of the permission you want to remove from the role.

  • relation (String) (defaults to: "member")

    The relation for this permission to user association. The relation must be valid as per the permission object type definition.

Returns:

  • (nil)

    if remove was successful

Raises:



242
243
244
# File 'lib/warrant/models/role.rb', line 242

def remove_permission(permission_id, relation: "member", options: {})
    return Permission.remove_from_role(role_id, permission_id, relation: relation, options: options)
end

#update(meta, options = {}) ⇒ Role

Updates a role with the given params

Examples:

Update role “test-role”‘s name

role = Warrant::Role.get("test-role")
role.update({ name: "Test Role" })

Parameters:

  • meta (Hash)

    A JSON object containing additional information about this role (e.g. name/description, etc.) to be persisted to Warrant.

Returns:

  • (Role)

    updated role

Raises:



128
129
130
# File 'lib/warrant/models/role.rb', line 128

def update(meta, options = {})
    return Role.update(role_id, meta)
end

#warrant_object_idObject



250
251
252
# File 'lib/warrant/models/role.rb', line 250

def warrant_object_id
    role_id
end

#warrant_object_typeObject



246
247
248
# File 'lib/warrant/models/role.rb', line 246

def warrant_object_type
    "role"
end