Module: Roles::Generic::User::Implementation
- Includes:
- RoleUtil
- Included in:
- Roles::Generic::User
- Defined in:
- lib/roles_generic/generic/user/multi_impl.rb,
lib/roles_generic/generic/user/single_impl.rb,
lib/roles_generic/generic/user/implementation.rb
Defined Under Namespace
Instance Method Summary collapse
-
#add_role(role) ⇒ Object
add a single role.
- #admin? ⇒ Boolean
- #exchange_role(role, options = {}) ⇒ Object
-
#exchange_roles(*role_names) ⇒ Object
should exchange the current role if in list with the first valid role in :with argument.
-
#has_any_role?(*roles_names) ⇒ Boolean
check if any of the roles listed have been assigned to that user.
- #has_only_role?(arg) ⇒ Boolean (also: #has_only?, #is_only?)
-
#has_role?(role_name) ⇒ Boolean
(also: #has?)
check if any (at least ONE) of the given roles have been assigned.
-
#has_roles?(*roles_names) ⇒ Boolean
(also: #is?)
check if all of the roles listed have been assigned to that user.
- #is_in_any_group?(*groups) ⇒ Boolean
-
#is_in_group?(group) ⇒ Boolean
(also: #is_member_of?)
is_in_group? :admin.
-
#is_in_groups?(*groups) ⇒ Boolean
is_in_groups? :editor, :admin,.
-
#remove_role(role) ⇒ Object
remove a single role.
-
#role=(role) ⇒ Object
set a single role.
- #role_attribute ⇒ Object
-
#roles ⇒ Object
query assigned roles.
-
#roles=(*role_names) ⇒ Object
assign multiple roles.
- #valid_role?(role) ⇒ Boolean
- #valid_roles ⇒ Object
- #valid_roles?(*roles) ⇒ Boolean
Methods included from RoleUtil
Instance Method Details
#add_role(role) ⇒ Object
add a single role
16 17 18 19 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 16 def add_role role raise ArgumentError, '#add_role takes a single role String or Symbol as the argument' if !role || role.kind_of?(Array) add_roles role end |
#admin? ⇒ Boolean
99 100 101 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 99 def admin? is? :admin end |
#exchange_role(role, options = {}) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 36 def exchange_role role, = {} raise ArgumentError, '#exchange_role takes a single role String or Symbol as the first argument' if !role || role.kind_of?(Array) raise ArgumentError, '#exchange_role takes a an options hash with a :with option as the last argument' if ! || ![:with] if [:with].kind_of?(Array) && self.class.role_strategy.multiplicity == :single raise ArgumentError, '#exchange_role should only take a single role to exchange with for a Role strategy with multiplicity of one' if [:with].size > 1 end exchange_roles role, end |
#exchange_roles(*role_names) ⇒ Object
should exchange the current role if in list with the first valid role in :with argument
28 29 30 31 32 33 34 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 28 def exchange_roles *role_names = last_option role_names raise ArgumentError, "Must take an options hash as last argument with a :with option signifying which role(s) to replace with" if ! || !.kind_of?(Hash) remove_roles(role_names.to_symbols) with_roles = [:with] add_roles(with_roles) end |
#has_any_role?(*roles_names) ⇒ Boolean
check if any of the roles listed have been assigned to that user
73 74 75 76 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 73 def has_any_role?(*roles_names) compare_roles = extract_roles(roles_names.flat_uniq) (roles_list & compare_roles).not.empty? end |
#has_only_role?(arg) ⇒ Boolean Also known as: has_only?, is_only?
123 124 125 126 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 123 def has_only_role? arg raise ArgumentError, "Must take only a single argument that is a role name" if arg.send(:size) > 1 && arg.kind_of?(Array) has_roles? [arg].flatten.first end |
#has_role?(role_name) ⇒ Boolean Also known as: has?
check if any (at least ONE) of the given roles have been assigned
79 80 81 82 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 79 def has_role? role_name raise ArgumentError, '#has_role? should take a single role String or Symbol as the argument' if !role_name || role_name.kind_of?(Array) has_roles? role_name end |
#has_roles?(*roles_names) ⇒ Boolean Also known as: is?
check if all of the roles listed have been assigned to that user
67 68 69 70 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 67 def has_roles?(*roles_names) compare_roles = extract_roles(roles_names.flat_uniq) (compare_roles - roles_list).empty? end |
#is_in_any_group?(*groups) ⇒ Boolean
61 62 63 64 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 61 def is_in_any_group? *groups groups = groups.flat_uniq groups.any? {|group| is_in_group? group} end |
#is_in_group?(group) ⇒ Boolean Also known as: is_member_of?
is_in_group? :admin
46 47 48 49 50 51 52 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 46 def is_in_group? group raise ArgumentError, 'Group id must be a String or Symbol' if !group.kind_of_label? group_roles = self.class.role_groups[group] # puts "group_roles: #{group_roles} for group: #{group}" # puts "roles_list: #{roles_list}" !(group_roles & roles_list).empty? end |
#is_in_groups?(*groups) ⇒ Boolean
is_in_groups? :editor, :admin,
56 57 58 59 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 56 def is_in_groups? *groups groups = groups.flat_uniq groups.all? {|group| is_in_group? group} end |
#remove_role(role) ⇒ Object
remove a single role
22 23 24 25 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 22 def remove_role role raise ArgumentError, '#remove_role takes a single role String or Symbol as the argument' if !role || role.kind_of?(Array) remove_roles role end |
#role=(role) ⇒ Object
set a single role
10 11 12 13 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 10 def role= role raise ArgumentError, '#add_role takes a single role String or Symbol as the argument' if !role || role.kind_of?(Array) self.roles = role end |
#role_attribute ⇒ Object
5 6 7 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 5 def role_attribute strategy_class.roles_attribute_name end |
#roles ⇒ Object
query assigned roles
112 113 114 115 116 117 118 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 112 def roles return [] if get_roles.nil? x = [get_roles].flatten.map do |role| role.respond_to?(:to_sym) ? role.to_sym : role end x.first.kind_of?(Set) ? x.first.to_a : x end |
#roles=(*role_names) ⇒ Object
assign multiple roles
104 105 106 107 108 109 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 104 def roles=(*role_names) role_names = role_names.flat_uniq role_names = extract_roles(role_names) return nil if role_names.empty? set_roles(select_valid_roles role_names) end |
#valid_role?(role) ⇒ Boolean
84 85 86 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 84 def valid_role? role strategy_class.valid_roles.include? role.to_sym end |
#valid_roles ⇒ Object
95 96 97 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 95 def valid_roles strategy_class.valid_roles end |
#valid_roles?(*roles) ⇒ Boolean
88 89 90 91 92 93 |
# File 'lib/roles_generic/generic/user/implementation.rb', line 88 def valid_roles? *roles roles.each do |role| return false if !valid_role? role end true end |