Class: Warrant::User
- Includes:
- WarrantObject
- Defined in:
- lib/warrant/models/user.rb
Constant Summary collapse
- OBJECT_TYPE =
"user"
Instance Attribute Summary
Attributes inherited from Object
#created_at, #meta, #object_id, #object_type
Class Method Summary collapse
-
.assign_to_tenant(tenant_id, user_id, relation: "member", options: {}) ⇒ Warrant
Add a user to a tenant.
-
.batch_create(users, options = {}) ⇒ Array<User>
Batch creates multiple users with given parameters.
-
.batch_delete(users, options = {}) ⇒ nil
Batch deletes multiple users with given parameters.
-
.create(params = {}, options = {}) ⇒ User
Creates a user with the given parameters.
-
.delete(user_id, options = {}) ⇒ nil
Deletes a user with given user id.
-
.get(user_id, options = {}) ⇒ User
Get a user with the given user_id.
-
.list(filters = {}, options = {}) ⇒ Array<User>
Lists all users for your organization.
-
.list_for_tenant(tenant_id, filters = {}, options = {}) ⇒ Array<User>
List all users for a tenant.
-
.remove_from_tenant(tenant_id, user_id, relation: "member", options: {}) ⇒ nil
Remove a user from a tenant.
-
.update(user_id, meta, options = {}) ⇒ User
Updates a user with the given user_id.
Instance Method Summary collapse
-
#assign_feature(feature_id, relation: "member", options: {}) ⇒ Feature
Assign a feature to a user.
-
#assign_permission(permission_id, relation: "member", options: {}) ⇒ Permission
Assign a permission to a user.
-
#assign_pricing_tier(pricing_tier_id, relation: "member", options: {}) ⇒ PricingTier
Assign a pricing tier to a user.
-
#assign_role(role_id, relation: "member", options: {}) ⇒ Permission
Assign a role to a user.
-
#has_feature?(feature_id, relation: "member", options: {}) ⇒ Boolean
Check whether a user has a given feature.
-
#has_permission?(permission_id, relation: "member", options: {}) ⇒ Boolean
Checks whether a user has a given permission.
-
#list_features(filters = {}, options = {}) ⇒ Array<Feature>
List features for a user.
-
#list_permissions(filters = {}, options = {}) ⇒ Array<Permission>
List all permissions for a user.
-
#list_pricing_tiers(filters = {}, options = {}) ⇒ Array<PricingTier>
List pricing tiers for a user.
-
#list_roles(filters = {}, options = {}) ⇒ Array<Role>
List all roles for a user.
-
#list_tenants(filters = {}, options = {}) ⇒ Array<Tenant>
List all tenants for a user.
-
#remove_feature(feature_id, relation: "member", options: {}) ⇒ nil
Remove a feature from a user.
-
#remove_permission(permission_id, relation: "member", options: {}) ⇒ nil
Remove a permission from a user.
-
#remove_pricing_tier(pricing_tier_id, relation: "member", options: {}) ⇒ nil
Remove a pricing tier from a user.
-
#remove_role(role_id, relation: "member", options: {}) ⇒ nil
Remove a role from a user.
-
#update(meta, options = {}) ⇒ User
Updates the user with the given params.
- #warrant_object_id ⇒ Object
- #warrant_object_type ⇒ Object
Class Method Details
.assign_to_tenant(tenant_id, user_id, relation: "member", options: {}) ⇒ Warrant
Add a user to a tenant
369 370 371 |
# File 'lib/warrant/models/user.rb', line 369 def self.assign_to_tenant(tenant_id, user_id, relation: "member", options: {}) Warrant.create({ object_type: Tenant::OBJECT_TYPE, object_id: tenant_id }, relation, { object_type: User::OBJECT_TYPE, object_id: user_id }, nil, ) end |
.batch_create(users, options = {}) ⇒ Array<User>
Batch creates multiple users with given parameters
54 55 56 57 |
# File 'lib/warrant/models/user.rb', line 54 def self.batch_create(users, = {}) res = Object.batch_create(users.map{ |user| { object_type: OBJECT_TYPE, object_id: user[:user_id], meta: user[:meta] }}, ) return res.map{ |obj| User.new(obj.object_id, obj., obj.created_at)} end |
.batch_delete(users, options = {}) ⇒ nil
Batch deletes multiple users with given parameters
90 91 92 93 94 95 96 97 98 |
# File 'lib/warrant/models/user.rb', line 90 def self.batch_delete(users, = {}) return Object.batch_delete(users.map{ |user| if user.instance_of? User { object_type: OBJECT_TYPE, object_id: user.object_id } else { object_type: OBJECT_TYPE, object_id: user[:user_id] } end }, ) end |
.create(params = {}, options = {}) ⇒ User
Creates a user with the given parameters
32 33 34 35 |
# File 'lib/warrant/models/user.rb', line 32 def self.create(params = {}, = {}) object = Object.create({ object_type: OBJECT_TYPE, object_id: params[:user_id], meta: params[:meta] }, ) return User.new(object.object_id, object., object.created_at) end |
.delete(user_id, options = {}) ⇒ nil
Deletes a user with given user id
72 73 74 |
# File 'lib/warrant/models/user.rb', line 72 def self.delete(user_id, = {}) return Object.delete(OBJECT_TYPE, user_id, ) end |
.get(user_id, options = {}) ⇒ User
Get a user with the given user_id
136 137 138 139 |
# File 'lib/warrant/models/user.rb', line 136 def self.get(user_id, = {}) object = Object.get(OBJECT_TYPE, user_id, ) return User.new(object.object_id, object., object.created_at) end |
.list(filters = {}, options = {}) ⇒ Array<User>
Lists all users for your organization
119 120 121 122 123 124 |
# File 'lib/warrant/models/user.rb', line 119 def self.list(filters = {}, = {}) filters.merge({ object_type: "user" }) list_response = Object.list(filters, ) users = list_response.results.map{ |object| User.new(object.object_id, object., object.created_at)} return ListResponse.new(users, list_response.prev_cursor, list_response.next_cursor) end |
.list_for_tenant(tenant_id, filters = {}, options = {}) ⇒ Array<User>
List all users for a tenant
350 351 352 353 354 |
# File 'lib/warrant/models/user.rb', line 350 def self.list_for_tenant(tenant_id, filters = {}, = {}) query_response = Warrant.query("select * of type user for tenant:#{tenant_id}", filters: filters, options: ) users = query_response.results.map{ |result| User.new(result.object_id, result.) } return ListResponse.new(users, query_response.prev_cursor, query_response.next_cursor) end |
.remove_from_tenant(tenant_id, user_id, relation: "member", options: {}) ⇒ nil
Remove a user from a tenant
385 386 387 |
# File 'lib/warrant/models/user.rb', line 385 def self.remove_from_tenant(tenant_id, user_id, relation: "member", options: {}) Warrant.delete({ object_type: Tenant::OBJECT_TYPE, object_id: tenant_id }, relation, { object_type: User::OBJECT_TYPE, object_id: user_id }, nil, ) end |
.update(user_id, meta, options = {}) ⇒ User
Updates a user with the given user_id
156 157 158 159 |
# File 'lib/warrant/models/user.rb', line 156 def self.update(user_id, , = {}) object = Object.update(OBJECT_TYPE, user_id, , ) return User.new(object.object_id, object., object.created_at) end |
Instance Method Details
#assign_feature(feature_id, relation: "member", options: {}) ⇒ Feature
Assign a feature to a user
497 498 499 |
# File 'lib/warrant/models/user.rb', line 497 def assign_feature(feature_id, relation: "member", options: {}) return Feature.assign_to_user(user_id, feature_id, relation: relation, options: ) end |
#assign_permission(permission_id, relation: "member", options: {}) ⇒ Permission
Assign a permission to a user
282 283 284 |
# File 'lib/warrant/models/user.rb', line 282 def (, relation: "member", options: {}) return Permission.assign_to_user(user_id, , relation: relation, options: ) end |
#assign_pricing_tier(pricing_tier_id, relation: "member", options: {}) ⇒ PricingTier
Assign a pricing tier to a user
444 445 446 |
# File 'lib/warrant/models/user.rb', line 444 def assign_pricing_tier(pricing_tier_id, relation: "member", options: {}) return PricingTier.assign_to_user(user_id, pricing_tier_id, relation: relation, options: ) end |
#assign_role(role_id, relation: "member", options: {}) ⇒ Permission
Assign a role to a user
219 220 221 |
# File 'lib/warrant/models/user.rb', line 219 def assign_role(role_id, relation: "member", options: {}) return Role.assign_to_user(user_id, role_id, relation: relation, options: ) end |
#has_feature?(feature_id, relation: "member", options: {}) ⇒ Boolean
Check whether a user has a given feature
530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/warrant/models/user.rb', line 530 def has_feature?(feature_id, relation: "member", options: {}) return Warrant.has_feature?({ feature_id: feature_id, relation: relation, subject: { object_type: "user", object_id: user_id }, context: [:context], debug: [:debug] }, ) end |
#has_permission?(permission_id, relation: "member", options: {}) ⇒ Boolean
Checks whether a user has a given permission
322 323 324 325 326 327 328 329 330 |
# File 'lib/warrant/models/user.rb', line 322 def (, relation: "member", options: {}) return Warrant.({ permission_id: , relation: relation, user_id: user_id, context: [:context], debug: [:debug] }, ) end |
#list_features(filters = {}, options = {}) ⇒ Array<Feature>
List features for a user
481 482 483 |
# File 'lib/warrant/models/user.rb', line 481 def list_features(filters = {}, = {}) return Feature.list_for_user(user_id, filters, ) end |
#list_permissions(filters = {}, options = {}) ⇒ Array<Permission>
List all permissions for a user
261 262 263 |
# File 'lib/warrant/models/user.rb', line 261 def (filters = {}, = {}) return Permission.list_for_user(user_id, filters, ) end |
#list_pricing_tiers(filters = {}, options = {}) ⇒ Array<PricingTier>
List pricing tiers for a user
428 429 430 |
# File 'lib/warrant/models/user.rb', line 428 def list_pricing_tiers(filters = {}, = {}) return PricingTier.list_for_user(user_id, filters, ) end |
#list_roles(filters = {}, options = {}) ⇒ Array<Role>
List all roles for a user.
197 198 199 |
# File 'lib/warrant/models/user.rb', line 197 def list_roles(filters = {}, = {}) return Role.list_for_user(user_id, filters, ) end |
#list_tenants(filters = {}, options = {}) ⇒ Array<Tenant>
List all tenants for a user
407 408 409 |
# File 'lib/warrant/models/user.rb', line 407 def list_tenants(filters = {}, = {}) return Tenant.list_for_user(user_id, filters, ) end |
#remove_feature(feature_id, relation: "member", options: {}) ⇒ nil
Remove a feature from a user
513 514 515 |
# File 'lib/warrant/models/user.rb', line 513 def remove_feature(feature_id, relation: "member", options: {}) return Feature.remove_from_user(user_id, feature_id, relation: relation, options: ) end |
#remove_permission(permission_id, relation: "member", options: {}) ⇒ nil
Remove a permission from a user
301 302 303 |
# File 'lib/warrant/models/user.rb', line 301 def (, relation: "member", options: {}) Permission.remove_from_user(user_id, , relation: relation, options: ) end |
#remove_pricing_tier(pricing_tier_id, relation: "member", options: {}) ⇒ nil
Remove a pricing tier from a user
460 461 462 |
# File 'lib/warrant/models/user.rb', line 460 def remove_pricing_tier(pricing_tier_id, relation: "member", options: {}) return PricingTier.remove_from_user(user_id, pricing_tier_id, relation: relation, options: ) end |
#remove_role(role_id, relation: "member", options: {}) ⇒ nil
Remove a role from a user
240 241 242 |
# File 'lib/warrant/models/user.rb', line 240 def remove_role(role_id, relation: "member", options: {}) return Role.remove_from_user(user_id, role_id, relation: relation, options: ) end |
#update(meta, options = {}) ⇒ User
Updates the user with the given params
176 177 178 |
# File 'lib/warrant/models/user.rb', line 176 def update(, = {}) return User.update(user_id, , ) end |
#warrant_object_id ⇒ Object
547 548 549 |
# File 'lib/warrant/models/user.rb', line 547 def warrant_object_id user_id end |
#warrant_object_type ⇒ Object
543 544 545 |
# File 'lib/warrant/models/user.rb', line 543 def warrant_object_type "user" end |