Class: Warrant::Permission

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

Constant Summary collapse

OBJECT_TYPE =
"permission"

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_role(role_id, permission_id, relation: "member", options: {}) ⇒ Warrant

Assign a permission to a role

Parameters:

  • role_id (String)

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

  • permission_id (String)

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

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

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

Returns:

  • (Warrant)

    warrant assigning permission to role

Raises:



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

def self.assign_to_role(role_id, permission_id, relation: "member", options: {})
    Warrant.create({ object_type: Permission::OBJECT_TYPE, object_id: permission_id }, relation, { object_type: Role::OBJECT_TYPE, object_id: role_id }, nil, options)
end

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

Assign a permission to a user

Parameters:

  • user_id (String)

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

  • permission_id (String)

    The permission_id of the permission you want to assign to a user.

  • 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:

  • (Warrant)

    warrant assigning permission to user

Raises:



229
230
231
# File 'lib/warrant/models/permission.rb', line 229

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

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

Creates a permission with the given parameters

Examples:

Create a new Permission with the permission id “test-permission”

Warrant::Permission.create(permission_id: "test-permission")

Parameters:

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

    a customizable set of options

Options Hash (params):

  • :role_id (String)

    User defined string identifier for this permission. If not provided, Warrant will create an id for the permission 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 permission. Note that permissionIds in Warrant must be composed of alphanumeric chars, ‘-’, and/or ‘_’. (optional)

  • :meta (Hash)

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

Returns:

Raises:



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

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

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

Deletes a permission with given permission id

Examples:

Delete a Permission with the permission id “test-permission”

Warrant::Permission.delete("test-permission")

Parameters:

  • permission_id (String)

    The permission_id of the permission to delete.

Returns:

  • (nil)

    if delete was successful

Raises:



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

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

.get(permission_id, options = {}) ⇒ Permission

Get a permission with the given permission_id

Parameters:

  • permission_id (String)

    The permission_id of the permission to retrieve.

Returns:

Raises:



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

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

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

Lists all permissions for your organization

Examples:

List all permissions

Warrant::Permission.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<Permission>)

    all permissions for your organization

Raises:



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

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

.list_for_role(role_id, filters = {}, options = {}) ⇒ Array<Permission>

List permissions for a role

Parameters:

  • role_id (String)

    The role_id of the role to list assigned permissions 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<Permission>)

    all assigned permissions for the role

Raises:



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

def self.list_for_role(role_id, filters = {}, options = {})
    query_response = Warrant.query("select permission where role:#{role_id} is *", filters: filters, options: options)
    permissions = query_response.results.map{ |result| Permission.new(result.object_id, result.meta) }
    return ListResponse.new(permissions, query_response.prev_cursor, query_response.next_cursor)
end

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

List permissions for a user

Parameters:

  • user_id (String)

    The user_id of the user to list assigned permissions 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<Permission>)

    all assigned permissions for the user

Raises:



209
210
211
212
213
# File 'lib/warrant/models/permission.rb', line 209

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

.remove_from_role(role_id, permission_id, relation: "member", options: {}) ⇒ nil

Remove a permission from a role

Parameters:

  • role_id (String)

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

  • permission_id (String)

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

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

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

Returns:

  • (nil)

    if remove was successful

Raises:



187
188
189
# File 'lib/warrant/models/permission.rb', line 187

def self.remove_from_role(role_id, permission_id, relation: "member", options: {})
    Warrant.delete({ object_type: Permission::OBJECT_TYPE, object_id: permission_id }, relation, { object_type: Role::OBJECT_TYPE, object_id: role_id }, nil, options)
end

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

Remove a permission from a user

Parameters:

  • user_id (String)

    The user_id of the user you want to remove a permission from.

  • permission_id (String)

    The permission_id of the permission you want to remove from a user.

  • 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:



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

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

.update(permission_id, meta, options = {}) ⇒ Permission

Updates a permission with the given role_id and params

Examples:

Update permission “test-permission”‘s name

Warrant::Permission.update("test-permission", { name: "Test Permission" })

Parameters:

  • permission_id (String)

    The permission_id of the permission to be updated.

  • meta (Hash)

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

Returns:

Raises:



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

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

Instance Method Details

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

Updates a permission with the given params

Examples:

Update permission “test-permission”‘s name

Warrant::Permission.update("test-permission", { name: "Test Permission" })

Parameters:

  • params (Hash)

    attributes to update user with

  • meta (Hash)

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

Returns:

Raises:



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

def update(meta, options = {})
    return Permission.update(permission_id, meta, options)
end

#warrant_object_idObject



254
255
256
# File 'lib/warrant/models/permission.rb', line 254

def warrant_object_id
    permission_id
end

#warrant_object_typeObject



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

def warrant_object_type
    "permission"
end