Class: Parse::Role
- Inherits:
-
Object
- Object
- Object
- Parse::Role
- Defined in:
- lib/parse/model/classes/role.rb,
lib/parse/stack/generators/templates/model_role.rb
Overview
This class represents the data and columns contained in the standard Parse _Role collection.
Roles allow the an application to group a set of User records with the same set of
permissions, so that specific records in the database can have ACLs related to a role
than trying to add all the users in a group.
The default schema for Role is as follows:
class Parse::Role < Parse::Object # See Parse::Object for inherited properties...
property :name
# A role may have child roles.
has_many :roles, through: :relation
# The set of users who belong to this role.
has_many :users, through: :relation
end
Constant Summary collapse
- MONGO_AVAILABILITY_ERROR_NAMES =
Names of Mongo driver errors that mean "the server is momentarily unreachable", for which falling back to the Parse Server walk is right.
Names rather than classes, resolved on first use rather than at load time, because the driver is required lazily (
Parse::MongoDB.require_gem!) andMongo::Errordoes not exist when this file loads.The previous check tested
Mongo::Error::ConnectionFailure, which does NOT exist in the locked 2.25.0 driver, sodefined?was always false and the fallback never fired on a real error. The tests manufactured the constant themselves, which is how a guard can be green and dead at the same time. Real availability errors propagated instead of degrading.ConnectionFailureis kept for older drivers that do define it. Deliberately narrow otherwise:ExecutionTimeout,DeniedOperator, andCLPScope::Deniedare attack signals or policy denials and must keep propagating rather than silently downgrading to the slow path. %w[ ConnectionFailure ConnectionUnavailable ConnectionPerished ConnectionCheckOutTimeout SocketError SocketTimeoutError NoServerAvailable ].freeze
Instance Attribute Summary collapse
-
#name ⇒ String
The name of this role.
Class Method Summary collapse
-
.all_for_user(user, max_depth: 10, master: false, as: nil, client: nil, strict: false) ⇒ Set<String>
Return the transitive upward closure of role names a user inherits permissions from.
-
.all_names ⇒ Array<String>
Get all role names in the system.
-
.exists?(role_name) ⇒ Boolean
Check if a role with the given name exists.
- .expand_inheritance_upward(starting_roles, max_depth: 10, client: nil, strict: false) ⇒ Object
-
.find_by_name(role_name) ⇒ Parse::Role?
Find a role by its name.
-
.find_or_create(role_name, acl: nil) ⇒ Parse::Role
Find or create a role by name.
-
.mongo_availability_error?(error) ⇒ Boolean
Whether
errormeans the server was unreachable. -
.mongo_availability_errors ⇒ Array<Class>
The subset of MONGO_AVAILABILITY_ERROR_NAMES this driver actually defines.
- .role_query_all(constraints, client: nil) ⇒ Object
Instance Method Summary collapse
-
#access_decision(object, operation, client: nil, authenticated: nil, max_role_depth: 10) ⇒ Parse::Access::Decision
Inspect one effective object permission for a hypothetical authenticated member of this role.
-
#access_decisions(object, client: nil, authenticated: nil, max_role_depth: 10) ⇒ Hash<Symbol, Parse::Access::Decision>
Inspect read, write, and delete while sharing one parent-role lookup.
-
#add_child_role(role) ⇒ self
Add a child role to this role's hierarchy.
-
#add_child_roles(*role_list) ⇒ self
Add multiple child roles to this role's hierarchy.
-
#add_user(user) ⇒ self
Add a single user to this role.
-
#add_users(*user_list) ⇒ self
Add multiple users to this role.
-
#all_child_roles(max_depth: 10, visited: Set.new) ⇒ Array<Parse::Role>
Get all child roles recursively.
-
#all_parent_role_names(max_depth: 10, client: nil, strict: false) ⇒ Set<String>
Get the set of role names whose presence in a
_rpermarray grants access to this role's members. -
#all_users(max_depth: 10, visited: Set.new, master: false, as: nil, client: nil) ⇒ Array<Parse::User>
Get all users belonging to this role, including users from child roles recursively.
-
#can_delete?(object, **options) ⇒ Boolean
Return whether the role-derived policy definitively grants delete access.
-
#can_read?(object, **options) ⇒ Boolean
Return whether the role-derived policy definitively grants read access.
-
#can_write?(object, **options) ⇒ Boolean
Return whether the role-derived policy definitively grants update access.
-
#child_roles_count ⇒ Integer
Get the count of direct child roles.
-
#grant_capabilities_to(grantee) ⇒ self
Grant this role's capabilities to the given role's users.
-
#grant_capabilities_to!(grantee) ⇒ self
Auto-saving variant of #grant_capabilities_to.
-
#has_child_role?(role) ⇒ Boolean
Check if a role is a direct child of this role.
-
#has_user?(user) ⇒ Boolean
Check if a user belongs to this role (direct membership only).
-
#inherits_capabilities_from(source) ⇒ Parse::Role
Inverse spelling of #grant_capabilities_to: "this role's users inherit
source's capabilities". -
#inherits_capabilities_from!(source) ⇒ self
Auto-saving variant of #inherits_capabilities_from.
-
#remove_child_role(role) ⇒ self
Remove a child role from this role's hierarchy.
-
#remove_child_roles(*role_list) ⇒ self
Remove multiple child roles from this role's hierarchy.
-
#remove_user(user) ⇒ self
Remove a single user from this role.
-
#remove_users(*user_list) ⇒ self
Remove multiple users from this role.
-
#roles ⇒ RelationCollectionProxy<Role>
This attribute is mapped as a
has_manyParse relation association with the Role class, as roles can be associated with multiple child roles to support role inheritance. -
#total_users_count ⇒ Integer
Get the total count of users including child roles.
-
#users ⇒ RelationCollectionProxy<User>
This attribute is mapped as a
has_manyParse relation association with the User class. -
#users_count ⇒ Integer
Get the count of direct users in this role.
Instance Attribute Details
#name ⇒ String
Returns the name of this role.
54 |
# File 'lib/parse/model/classes/role.rb', line 54 property :name |
Class Method Details
.all_for_user(user, max_depth: 10, master: false, as: nil, client: nil, strict: false) ⇒ Set<String>
When neither master: nor as: is supplied, the
mongo-direct fast path is skipped; the method falls
through to the Parse-Server walk
(Parse::Role.all(users: user_pointer)) which goes through
the default Parse::Client. This preserves backward
compatibility for the many SDK-internal call sites that
compose ACL scopes (acl_scope, atlas_search session,
query/constraints) — none of those have a caller scope to
forward. The fast path is opt-in for performance-conscious
callers that can supply explicit authorization.
Inside Parse.without_master_key, the Parse Server walk (no
master: or as:) still reads _Role with the master key as SDK
metadata, so it returns the full role-name closure of user, not
only the publicly readable roles. The block guards against
accidental master-key use; it does not scope this lookup. Pass
as: for a scope-checked answer.
Return the transitive upward closure of role names a user inherits permissions from.
Parse Server _Role inheritance: when role X holds role Y
in its roles relation, users of Y inherit X's
permissions. So given a user U, the permission set is built
by:
- Querying for every role
DwhereUis a direct member (_Role.userscontainsU). - For each direct role
D, walking upward to every rolePthat listsDin itsrolesrelation. Repeat until no new parents are found.
This is the correct primitive for building _rperm predicates
(e.g., ACLReadableByConstraint, ACLWritableByConstraint,
and the Atlas Search ACL $match injection). The legacy walk
via #all_child_roles on the user's direct roles traverses
the wrong direction and over-grants — it returns roles whose
users include the input user through inheritance, not the
roles the input user inherits permissions from.
Cycle-safe: a visited-id set guards against pathological
_Role.roles cycles (e.g. A→B→A).
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/parse/model/classes/role.rb', line 291 def all_for_user(user, max_depth: 10, master: false, as: nil, client: nil, strict: false) names = Set.new return names if user.nil? || max_depth <= 0 user_pointer = role_lookup_pointer_for(user) return names if user_pointer.nil? # The fast path is opt-in. When neither `master:` nor `as:` is # supplied, skip it entirely — the underlying mongo helper # would raise ArgumentError, and we don't want to surprise # the many backward-compat call sites (acl_scope.resolve_for_user, # atlas_search Session.role_names_for, query/constraints' ACL # constraint building, agent default-scope composition) that # have no scope to forward. if master == true || !as.nil? fast_path_result = all_for_user_mongo_fast_path( user_pointer.id, max_depth, master: master, as: as, client: client, ) if fast_path_result.is_a?(Set) ActiveSupport::Notifications.instrument( "parse.role.expand", direction: :forward, target_id: user_pointer.id, depth: max_depth, source: :mongo_direct, result_count: fast_path_result.size, ) return fast_path_result end end if as # FAIL CLOSED. The slow path below reads `_Role` through the REST # client, which in a master-keyed process answers with every role # regardless of what `as:` may see. Silently falling back would turn # a scoped traversal into a master-keyed one and hand the caller a # closure they were never entitled to. A caller who wants the master # answer has to ask for it. raise Parse::MongoDB::NotEnabled, "Parse::Role.all_for_user: `as:` requires the mongo-direct role graph, " \ "which is unavailable. Refusing to fall back to the Parse Server walk, " \ "which would run under the client's own credentials rather than the " \ "requested scope. Configure Parse::MongoDB, or pass `master: true` to " \ "take the unscoped answer deliberately." end begin direct_roles = role_query_all({ users: user_pointer }, client: client) rescue StandardError raise if strict return names end result = ( direct_roles, max_depth: max_depth, client: client, strict: strict, ) ActiveSupport::Notifications.instrument( "parse.role.expand", direction: :forward, target_id: user_pointer.id, depth: max_depth, source: :parse_server, result_count: result.size, ) result end |
.all_names ⇒ Array<String>
Get all role names in the system.
216 217 218 |
# File 'lib/parse/model/classes/role.rb', line 216 def all_names query.results.map(&:name) end |
.exists?(role_name) ⇒ Boolean
Check if a role with the given name exists.
223 224 225 |
# File 'lib/parse/model/classes/role.rb', line 223 def exists?(role_name) query(name: role_name).count > 0 end |
.expand_inheritance_upward(starting_roles, max_depth: 10, client: nil, strict: false) ⇒ Object
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/parse/model/classes/role.rb', line 460 def (starting_roles, max_depth: 10, client: nil, strict: false) names = Set.new visited_ids = Set.new frontier = [] Array(starting_roles).each do |role| next if role.nil? || role.id.nil? next if visited_ids.include?(role.id) visited_ids << role.id names << role.name if role.respond_to?(:name) && role.name.present? frontier << role end depth = 0 while frontier.any? && depth < max_depth next_frontier = [] frontier.each do |role| next if role.nil? || role.id.nil? begin parents = role_query_all({ roles: role }, client: client) rescue StandardError raise if strict next end parents.each do |parent| next if parent.nil? || parent.id.nil? next if visited_ids.include?(parent.id) visited_ids << parent.id names << parent.name if parent.respond_to?(:name) && parent.name.present? next_frontier << parent end end frontier = next_frontier depth += 1 end names end |
.find_by_name(role_name) ⇒ Parse::Role?
Find a role by its name.
194 195 196 |
# File 'lib/parse/model/classes/role.rb', line 194 def find_by_name(role_name) query(name: role_name).first end |
.find_or_create(role_name, acl: nil) ⇒ Parse::Role
Find or create a role by name.
204 205 206 207 208 209 210 211 212 |
# File 'lib/parse/model/classes/role.rb', line 204 def find_or_create(role_name, acl: nil) role = find_by_name(role_name) return role if role role = new(name: role_name) role.acl = acl if acl role.save role end |
.mongo_availability_error?(error) ⇒ Boolean
Returns whether error means the server was unreachable.
174 175 176 |
# File 'lib/parse/model/classes/role.rb', line 174 def self.mongo_availability_error?(error) mongo_availability_errors.any? { |klass| error.is_a?(klass) } end |
.mongo_availability_errors ⇒ Array<Class>
Returns the subset of MONGO_AVAILABILITY_ERROR_NAMES this driver actually defines.
166 167 168 169 170 171 |
# File 'lib/parse/model/classes/role.rb', line 166 def self.mongo_availability_errors return [] unless defined?(::Mongo::Error) @mongo_availability_errors ||= MONGO_AVAILABILITY_ERROR_NAMES.filter_map do |name| ::Mongo::Error.const_get(name) if ::Mongo::Error.const_defined?(name) end.freeze end |
.role_query_all(constraints, client: nil) ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/parse/model/classes/role.rb', line 431 def role_query_all(constraints, client: nil) # Inside `Parse.without_master_key` the role graph is still read with # the master key. It is metadata the SDK needs to enforce a scope, as # Parse Server reads it itself; read without the key it returned only # publicly readable roles, and that short closure was then cached for # the user and served to every caller until it expired. if Parse.respond_to?(:master_key_disabled?) && Parse.master_key_disabled? query = Parse::Role.query(constraints.reverse_merge(limit: :max)) query.client = client if client # A master-key read: keep it out of the shared response cache. query.cache = false query.instance_variable_set(:@_metadata_master, true) return query.results end # No explicit client means the historical path, unchanged. This is not # only for compatibility: `Parse::Role.all` is what callers and tests # observe and stub, and routing around it when nothing asked us to # would change behavior for every existing caller to fix a problem # none of them have. # Double-splat, not a positional Hash: `Parse::Role.all` takes # keywords, and Ruby 3 does not convert one to the other. return Parse::Role.all(**constraints) if client.nil? query = Parse::Role.query(constraints) query.client = client query.results end |
Instance Method Details
#access_decision(object, operation, client: nil, authenticated: nil, max_role_depth: 10) ⇒ Parse::Access::Decision
Inspect one effective object permission for a hypothetical authenticated
member of this role. User-specific, pointer-specific, and _User self
rules remain unknown because a role does not identify a concrete member.
901 902 903 904 905 906 907 908 909 910 911 912 |
# File 'lib/parse/model/classes/role.rb', line 901 def access_decision(object, operation, client: nil, authenticated: nil, max_role_depth: 10) require_relative "../../access" unless defined?(Parse::Access) Parse::Access.check( principal: self, object: object, operation: operation, client: client, authenticated: authenticated, max_role_depth: max_role_depth, ) end |
#access_decisions(object, client: nil, authenticated: nil, max_role_depth: 10) ⇒ Hash<Symbol, Parse::Access::Decision>
Inspect read, write, and delete while sharing one parent-role lookup.
916 917 918 919 920 921 922 923 924 925 |
# File 'lib/parse/model/classes/role.rb', line 916 def access_decisions(object, client: nil, authenticated: nil, max_role_depth: 10) require_relative "../../access" unless defined?(Parse::Access) Parse::Access.check_all( principal: self, object: object, client: client, authenticated: authenticated, max_role_depth: max_role_depth, ) end |
#add_child_role(role) ⇒ self
Add a child role to this role's hierarchy.
The method name is misleading — prefer #grant_capabilities_to!
or #inherits_capabilities_from!. add_child_role mutates the
receiver's roles relation; per Parse Server semantics, putting
role Y in role X's roles relation grants X's capabilities to
users-of-Y. The "child" terminology has the inheritance direction
exactly inverted from intuitive org-chart reading. Retained for
backward compatibility and as the low-level structural primitive;
new callers should use the direction-explicit semantic methods.
IMPORTANT — Parse Server _Role inheritance semantics: when role X
holds role Y in its roles relation, users of Y inherit X's
permissions (not the other way around). So calling
admin.add_child_role(moderator) does NOT grant Moderator's
capabilities to Admin; it grants Admin's capabilities to every
Moderator user — privilege escalation.
If you want Admins to have everything Moderators can do, you need to add ADMIN to MODERATOR's roles relation:
moderator.add_child_role(admin) # Admins now have Moderator capabilities
Direction-explicit replacements:
admin.inherits_capabilities_from!(moderator) # admin perspective moderator.grant_capabilities_to!(admin) # moderator perspective
Both bang variants auto-save and return self.
594 595 596 597 598 |
# File 'lib/parse/model/classes/role.rb', line 594 def add_child_role(role) assert_not_self_reference!(role, :add_child_role) roles.add(role) self end |
#add_child_roles(*role_list) ⇒ self
Add multiple child roles to this role's hierarchy. See #add_child_role for the inheritance-direction caveat.
605 606 607 608 609 610 |
# File 'lib/parse/model/classes/role.rb', line 605 def add_child_roles(*role_list) flat = role_list.flatten flat.each { |r| assert_not_self_reference!(r, :add_child_roles) } roles.add(flat) self end |
#add_user(user) ⇒ self
Add a single user to this role.
530 531 532 533 |
# File 'lib/parse/model/classes/role.rb', line 530 def add_user(user) users.add(user) self end |
#add_users(*user_list) ⇒ self
Add multiple users to this role.
540 541 542 543 |
# File 'lib/parse/model/classes/role.rb', line 540 def add_users(*user_list) users.add(user_list.flatten) self end |
#all_child_roles(max_depth: 10, visited: Set.new) ⇒ Array<Parse::Role>
Get all child roles recursively. Cycle-safe; see #all_users.
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 |
# File 'lib/parse/model/classes/role.rb', line 993 def all_child_roles(max_depth: 10, visited: Set.new) return [] if max_depth <= 0 return [] if id.nil? || visited.include?(id) visited << id direct_children = roles.all nested_children = direct_children.flat_map do |child| child.all_child_roles(max_depth: max_depth - 1, visited: visited) end (direct_children + nested_children).uniq { |r| r.id } end |
#all_parent_role_names(max_depth: 10, client: nil, strict: false) ⇒ Set<String>
Inside Parse.without_master_key the walk still reads _Role
with the master key as SDK metadata, so it returns the full parent
closure, not only publicly readable roles.
Get the set of role names whose presence in a _rperm array
grants access to this role's members. That's the role itself
plus every role P that lists this role in its roles relation,
transitively upward — because users of this role inherit P's
permissions under Parse Server's role-inheritance semantics
(see #add_child_role).
The instance-side analogue to all_for_user; the
two share an internal BFS via
expand_inheritance_upward. Use this method when
compiling an ACL predicate around a role argument, e.g.
:ACL.readable_by => admin_role: the role itself contributes
"role:Admin", and any role whose .roles relation contains
admin_role also grants Admins access through inheritance.
The legacy #all_child_roles walk is NOT a substitute. Child
roles inherit FROM this role (their members get this role's
capabilities), so child-role names in _rperm would not grant
this role's members anything — the walk traverses the wrong
direction for ACL composition.
983 984 985 986 987 |
# File 'lib/parse/model/classes/role.rb', line 983 def all_parent_role_names(max_depth: 10, client: nil, strict: false) Parse::Role.( [self], max_depth: max_depth, client: client, strict: strict, ) end |
#all_users(max_depth: 10, visited: Set.new, master: false, as: nil, client: nil) ⇒ Array<Parse::User>
When neither master: nor as: is supplied, the
mongo-direct fast path is skipped; the method falls through
to the Parse-Server walk through the per-relation query
interface, which goes through the default Parse::Client.
Get all users belonging to this role, including users from child roles recursively.
Cycle-safe: a visited set guards against pathological
_Role.roles cycles (e.g. A→B→A) that would otherwise cause
exponential per-node query fan-out.
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 |
# File 'lib/parse/model/classes/role.rb', line 754 def all_users(max_depth: 10, visited: Set.new, master: false, as: nil, client: nil) return [] if max_depth <= 0 return [] if id.nil? || visited.include?(id) # The fast path is opt-in (same rationale as {.all_for_user}). if master == true || !as.nil? fast_path = all_users_mongo_fast_path(max_depth, master: master, as: as, client: client) if fast_path.is_a?(Array) ActiveSupport::Notifications.instrument( "parse.role.expand", direction: :reverse, target_id: id, depth: max_depth, source: :mongo_direct, result_count: fast_path.size, ) return fast_path end end if as # Same fail-closed rule as {.all_for_user}: the relation reads below # run under the client's own credentials, so a master-keyed process # would answer a scoped request with every user in the subtree. raise Parse::MongoDB::NotEnabled, "Parse::Role#all_users: `as:` requires the mongo-direct role graph, " \ "which is unavailable. Refusing to fall back to the Parse Server walk, " \ "which would run under the client's own credentials rather than the " \ "requested scope. Configure Parse::MongoDB, or pass `master: true` to " \ "take the unscoped answer deliberately." end visited << id # The posture travels with the recursion. Dropping `client:` here meant # a traversal that started on client B finished on the default, and # dropping `master:` meant an explicit master call could silently # downgrade under client mode or an ambient session token partway down # the tree. direct_users = relation_all(users, client: client) child_roles = relation_all(roles, client: client) child_users = child_roles.flat_map do |child_role| child_role.all_users( max_depth: max_depth - 1, visited: visited, master: master, as: as, client: client, ) end result = (direct_users + child_users).uniq { |u| u.id } ActiveSupport::Notifications.instrument( "parse.role.expand", direction: :reverse, target_id: id, depth: max_depth, source: :parse_server, result_count: result.size, ) result end |
#can_delete?(object, **options) ⇒ Boolean
Return whether the role-derived policy definitively grants delete access.
942 943 944 |
# File 'lib/parse/model/classes/role.rb', line 942 def can_delete?(object, **) access_decision(object, :delete, **).allowed? end |
#can_read?(object, **options) ⇒ Boolean
Return whether the role-derived policy definitively grants read access. Unknown states fail closed.
930 931 932 |
# File 'lib/parse/model/classes/role.rb', line 930 def can_read?(object, **) access_decision(object, :read, **).allowed? end |
#can_write?(object, **options) ⇒ Boolean
Return whether the role-derived policy definitively grants update access.
936 937 938 |
# File 'lib/parse/model/classes/role.rb', line 936 def can_write?(object, **) access_decision(object, :write, **).allowed? end |
#child_roles_count ⇒ Integer
Get the count of direct child roles.
1014 1015 1016 |
# File 'lib/parse/model/classes/role.rb', line 1014 def child_roles_count roles.query.count end |
#grant_capabilities_to(grantee) ⇒ self
Grant this role's capabilities to the given role's users. Reads as:
"users with grantee now have self's capabilities."
Equivalent to self.add_child_role(grantee) but unambiguous about
the direction of inheritance.
Non-saving — the caller must call self.save to persist. See
#grant_capabilities_to! for the auto-saving variant.
641 642 643 644 645 |
# File 'lib/parse/model/classes/role.rb', line 641 def grant_capabilities_to(grantee) assert_not_self_reference!(grantee, :grant_capabilities_to) roles.add(grantee) self end |
#grant_capabilities_to!(grantee) ⇒ self
Auto-saving variant of #grant_capabilities_to. Performs the
relation mutation AND persists self in one call. Returns self
consistently so the caller can chain or store the result without
tracking which object was mutated. Prefer this in tests and
one-shot scripts where batching multiple mutations isn't needed.
659 660 661 662 663 |
# File 'lib/parse/model/classes/role.rb', line 659 def grant_capabilities_to!(grantee) grant_capabilities_to(grantee) save! self end |
#has_child_role?(role) ⇒ Boolean
Check if a role is a direct child of this role.
721 722 723 724 |
# File 'lib/parse/model/classes/role.rb', line 721 def has_child_role?(role) return false unless role.is_a?(Parse::Role) && role.id.present? roles.query.where(objectId: role.id).count > 0 end |
#has_user?(user) ⇒ Boolean
Check if a user belongs to this role (direct membership only).
713 714 715 716 |
# File 'lib/parse/model/classes/role.rb', line 713 def has_user?(user) return false unless user.is_a?(Parse::User) && user.id.present? users.query.where(objectId: user.id).count > 0 end |
#inherits_capabilities_from(source) ⇒ Parse::Role
Inverse spelling of #grant_capabilities_to: "this role's users
inherit source's capabilities". Performs the relation mutation
on source, not on self.
Save target. The mutation lives on source.roles. To persist,
the caller must save source, NOT self. This asymmetry exists
because Parse Server stores the relation on the role that holds
the roles list, and that role is source. The non-bang form is
retained for callers that need to batch multiple mutations on
source before a single save; prefer #inherits_capabilities_from!
for the one-shot case where the auto-save matches intent.
686 687 688 689 690 |
# File 'lib/parse/model/classes/role.rb', line 686 def inherits_capabilities_from(source) assert_not_self_reference!(source, :inherits_capabilities_from) source.roles.add(self) source end |
#inherits_capabilities_from!(source) ⇒ self
Auto-saving variant of #inherits_capabilities_from. Performs the
mutation on source.roles AND saves source for you, then
returns self so the caller can keep working with the role they
called the method on. Resolves the most common stumbling block
with #inherits_capabilities_from: the "save target" asymmetry.
704 705 706 707 708 |
# File 'lib/parse/model/classes/role.rb', line 704 def inherits_capabilities_from!(source) inherits_capabilities_from(source) source.save! self end |
#remove_child_role(role) ⇒ self
Remove a child role from this role's hierarchy.
615 616 617 618 |
# File 'lib/parse/model/classes/role.rb', line 615 def remove_child_role(role) roles.remove(role) self end |
#remove_child_roles(*role_list) ⇒ self
Remove multiple child roles from this role's hierarchy.
623 624 625 626 |
# File 'lib/parse/model/classes/role.rb', line 623 def remove_child_roles(*role_list) roles.remove(role_list.flatten) self end |
#remove_user(user) ⇒ self
Remove a single user from this role.
548 549 550 551 |
# File 'lib/parse/model/classes/role.rb', line 548 def remove_user(user) users.remove(user) self end |
#remove_users(*user_list) ⇒ self
Remove multiple users from this role.
556 557 558 559 |
# File 'lib/parse/model/classes/role.rb', line 556 def remove_users(*user_list) users.remove(user_list.flatten) self end |
#roles ⇒ RelationCollectionProxy<Role>
This attribute is mapped as a has_many Parse relation association with the Parse::Role class,
as roles can be associated with multiple child roles to support role inheritance.
The roles Parse relation provides a mechanism to create a hierarchical inheritable types of permissions
by assigning child roles.
60 |
# File 'lib/parse/model/classes/role.rb', line 60 has_many :roles, through: :relation |
#total_users_count ⇒ Integer
Get the total count of users including child roles.
1020 1021 1022 |
# File 'lib/parse/model/classes/role.rb', line 1020 def total_users_count all_users.count end |
#users ⇒ RelationCollectionProxy<User>
This attribute is mapped as a has_many Parse relation association with the User class.
63 |
# File 'lib/parse/model/classes/role.rb', line 63 has_many :users, through: :relation |
#users_count ⇒ Integer
Get the count of direct users in this role.
1008 1009 1010 |
# File 'lib/parse/model/classes/role.rb', line 1008 def users_count users.query.count end |