Class: Warrant::Role
- 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
-
.assign_to_user(user_id, role_id, relation: "member", options: {}) ⇒ Warrant
Assign a role to a user.
-
.create(params = {}, options = {}) ⇒ Role
Creates a role with the given parameters.
-
.delete(role_id, options = {}) ⇒ nil
Deletes a role with given role id.
-
.get(role_id, options = {}) ⇒ Role
Get a role with the given role_id.
-
.list(filters = {}, options = {}) ⇒ Array<Role>
Lists all roles for your organization.
-
.list_for_user(user_id, filters = {}, options = {}) ⇒ Array<Role>
List roles for user.
-
.remove_from_user(user_id, role_id, relation: "member", options: {}) ⇒ nil
Remove a role from a user.
-
.update(role_id, meta, options = {}) ⇒ Role
Updates a role with the given role_id and params.
Instance Method Summary collapse
-
#assign_permission(permission_id, relation: "member", options: {}) ⇒ Permission
Assign a permission to a role.
-
#list_permissions(filters = {}, options = {}) ⇒ Permission
List assigned permissions for the role.
-
#remove_permission(permission_id, relation: "member", options: {}) ⇒ nil
Remove a permission from a role.
-
#update(meta, options = {}) ⇒ Role
Updates a role with the given params.
- #warrant_object_id ⇒ Object
- #warrant_object_type ⇒ Object
Methods inherited from Object
Class Method Details
.assign_to_user(user_id, role_id, relation: "member", options: {}) ⇒ Warrant
Assign a role to a user
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, ) end |
.create(params = {}, options = {}) ⇒ Role
Creates a role with the given parameters
30 31 32 33 |
# File 'lib/warrant/models/role.rb', line 30 def self.create(params = {}, = {}) object = Object.create({ object_type: OBJECT_TYPE, object_id: params[:role_id], meta: params[:meta] }, ) return Role.new(object.object_id, object., object.created_at) end |
.delete(role_id, options = {}) ⇒ nil
Deletes a role with given role id
48 49 50 |
# File 'lib/warrant/models/role.rb', line 48 def self.delete(role_id, = {}) return Object.delete(OBJECT_TYPE, role_id, ) end |
.get(role_id, options = {}) ⇒ Role
Get a role with the given role_id
88 89 90 91 |
# File 'lib/warrant/models/role.rb', line 88 def self.get(role_id, = {}) object = Object.get(OBJECT_TYPE, role_id, ) return Role.new(object.object_id, object., object.created_at) end |
.list(filters = {}, options = {}) ⇒ Array<Role>
Lists all roles for your organization
71 72 73 74 75 76 |
# File 'lib/warrant/models/role.rb', line 71 def self.list(filters = {}, = {}) filters.merge({ object_type: "role" }) list_response = Object.list(filters, ) roles = list_response.results.map{ |object| Role.new(object.object_id, object., 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
150 151 152 153 154 |
# File 'lib/warrant/models/role.rb', line 150 def self.list_for_user(user_id, filters = {}, = {}) query_response = Warrant.query("select role where user:#{user_id} is *", filters: filters, options: ) roles = query_response.results.map{ |result| Role.new(result.object_id, result.) } 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
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, ) end |
.update(role_id, meta, options = {}) ⇒ Role
Updates a role with the given role_id and params
108 109 110 111 |
# File 'lib/warrant/models/role.rb', line 108 def self.update(role_id, , = {}) object = Object.update(OBJECT_TYPE, role_id, , ) return Role.new(object.object_id, object., object.created_at) end |
Instance Method Details
#assign_permission(permission_id, relation: "member", options: {}) ⇒ Permission
Assign a permission to a role
226 227 228 |
# File 'lib/warrant/models/role.rb', line 226 def (, relation: "member", options: {}) return Permission.assign_to_role(role_id, , relation: relation, options: ) end |
#list_permissions(filters = {}, options = {}) ⇒ Permission
List assigned permissions for the role
209 210 211 |
# File 'lib/warrant/models/role.rb', line 209 def (filters = {}, = {}) return Permission.list_for_role(role_id, filters, ) end |
#remove_permission(permission_id, relation: "member", options: {}) ⇒ nil
Remove a permission from a role
242 243 244 |
# File 'lib/warrant/models/role.rb', line 242 def (, relation: "member", options: {}) return Permission.remove_from_role(role_id, , relation: relation, options: ) end |
#update(meta, options = {}) ⇒ Role
Updates a role with the given params
128 129 130 |
# File 'lib/warrant/models/role.rb', line 128 def update(, = {}) return Role.update(role_id, ) end |
#warrant_object_id ⇒ Object
250 251 252 |
# File 'lib/warrant/models/role.rb', line 250 def warrant_object_id role_id end |
#warrant_object_type ⇒ Object
246 247 248 |
# File 'lib/warrant/models/role.rb', line 246 def warrant_object_type "role" end |