Class: Organizations::Membership
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Organizations::Membership
- Extended by:
- MetadataFlags
- Defined in:
- lib/organizations/models/membership.rb
Overview
Membership model representing a user's membership in an organization. Each membership has a role that determines permissions.
The role hierarchy is: owner > admin > member > viewer Each role inherits permissions from lower roles.
⚠️ THE MEMBERSHIP GATE IS NOT ON THIS MODEL. on_member_joining (the
strict, vetoing host callback — seat limits, member caps) is dispatched
by the SANCTIONED join paths: Organization#add_member!,
Invitation#accept!, and JoinRequest#approve! (which covers codes,
domains, allowlists, and the account-email shortcut). A direct
Membership.create! bypasses the gate entirely — that's deliberate for
ops/console/test usage (Organization.create_with_owner!, TestHelpers),
but NEVER create memberships directly from request-cycle product code,
and any NEW join path added to the gem must dispatch the gate itself
(see Configuration#on_member_joining).
Defined Under Namespace
Classes: CannotDemoteOwner, CannotPromoteToOwner, InvalidRoleChange
Instance Method Summary collapse
-
#admin? ⇒ Boolean
Check if this membership is for an admin (not owner).
-
#compare_role(other) ⇒ Integer
Compare roles with another membership.
-
#demote_to!(new_role, changed_by: nil) ⇒ self
Demote to a lower role.
-
#has_permission_to?(permission) ⇒ Boolean
Check if this membership has a specific permission Uses pre-computed permission sets for O(1) lookup.
-
#is_at_least?(minimum_role) ⇒ Boolean
Check if this membership's role is at least as high as the specified role.
-
#member? ⇒ Boolean
Check if this membership is for a member.
-
#owner? ⇒ Boolean
Check if this membership is for an owner.
-
#permissions ⇒ Array<Symbol>
Get all permissions for this membership.
-
#promote_to!(new_role, changed_by: nil) ⇒ self
Promote to a higher role.
-
#role_sym ⇒ Symbol
Get the role as a symbol.
-
#verified? ⇒ Boolean
Whether this membership was created with a proven email address (emailed-code challenge, confirmed account email, or accepted invitation).
-
#viewer? ⇒ Boolean
Check if this membership is for a viewer.
Methods included from MetadataFlags
Instance Method Details
#admin? ⇒ Boolean
Check if this membership is for an admin (not owner)
118 119 120 |
# File 'lib/organizations/models/membership.rb', line 118 def admin? role_sym == :admin end |
#compare_role(other) ⇒ Integer
Compare roles with another membership
165 166 167 |
# File 'lib/organizations/models/membership.rb', line 165 def compare_role(other) Roles.compare(role_sym, other.role_sym) end |
#demote_to!(new_role, changed_by: nil) ⇒ self
Demote to a lower role
199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/organizations/models/membership.rb', line 199 def demote_to!(new_role, changed_by: nil) new_role_sym = new_role.to_sym validate_role!(new_role_sym) if owner? raise CannotDemoteOwner, Organizations.t(:"errors.cannot_demote_owner") end unless Roles.at_least?(role_sym, new_role_sym) raise InvalidRoleChange, Organizations.t(:"errors.demote_not_lower", new_role: new_role, role: role) end change_role_to!(new_role_sym, changed_by: changed_by) end |
#has_permission_to?(permission) ⇒ Boolean
Check if this membership has a specific permission Uses pre-computed permission sets for O(1) lookup
140 141 142 |
# File 'lib/organizations/models/membership.rb', line 140 def () Roles.(role_sym, ) end |
#is_at_least?(minimum_role) ⇒ Boolean
Check if this membership's role is at least as high as the specified role
158 159 160 |
# File 'lib/organizations/models/membership.rb', line 158 def is_at_least?(minimum_role) Roles.at_least?(role_sym, minimum_role.to_sym) end |
#member? ⇒ Boolean
Check if this membership is for a member
124 125 126 |
# File 'lib/organizations/models/membership.rb', line 124 def member? role_sym == :member end |
#owner? ⇒ Boolean
Check if this membership is for an owner
112 113 114 |
# File 'lib/organizations/models/membership.rb', line 112 def owner? role_sym == :owner end |
#permissions ⇒ Array<Symbol>
Get all permissions for this membership
146 147 148 |
# File 'lib/organizations/models/membership.rb', line 146 def Roles.(role_sym) end |
#promote_to!(new_role, changed_by: nil) ⇒ self
Promote to a higher role
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/organizations/models/membership.rb', line 177 def promote_to!(new_role, changed_by: nil) new_role_sym = new_role.to_sym validate_role!(new_role_sym) # Owner role is only assignable via transfer_ownership_to! if new_role_sym == :owner raise CannotPromoteToOwner, Organizations.t(:"errors.cannot_promote_to_owner") end unless Roles.at_least?(new_role_sym, role_sym) raise InvalidRoleChange, Organizations.t(:"errors.promote_not_higher", new_role: new_role, role: role) end change_role_to!(new_role_sym, changed_by: changed_by) end |
#role_sym ⇒ Symbol
Get the role as a symbol
106 107 108 |
# File 'lib/organizations/models/membership.rb', line 106 def role_sym role&.to_sym end |
#verified? ⇒ Boolean
Whether this membership was created with a proven email address (emailed-code challenge, confirmed account email, or accepted invitation)
98 99 100 |
# File 'lib/organizations/models/membership.rb', line 98 def verified? verified_at.present? end |
#viewer? ⇒ Boolean
Check if this membership is for a viewer
130 131 132 |
# File 'lib/organizations/models/membership.rb', line 130 def viewer? role_sym == :viewer end |