Class: Lita::Authorization
- Inherits:
-
Object
- Object
- Lita::Authorization
- Defined in:
- lib/lita/authorization.rb
Overview
Methods for querying and manipulating authorization groups.
Class Method Summary collapse
-
.add_user_to_group(*args) ⇒ Object
deprecated
Deprecated.
Will be removed in Lita 5.0 Use #add_user_to_group instead.
-
.groups(*args) ⇒ Object
deprecated
Deprecated.
Will be removed in Lita 5.0 Use #groups instead.
-
.groups_with_users(*args) ⇒ Object
deprecated
Deprecated.
Will be removed in Lita 5.0 Use #groups_with_users instead.
-
.remove_user_from_group(*args) ⇒ Object
deprecated
Deprecated.
Will be removed in Lita 5.0 Use #remove_user_from_group instead.
-
.user_in_group?(*args) ⇒ Object
deprecated
Deprecated.
Will be removed in Lita 5.0 Use #user_in_group? instead.
-
.user_is_admin?(*args) ⇒ Object
deprecated
Deprecated.
Will be removed in Lita 5.0 Use #user_is_admin? instead.
Instance Method Summary collapse
-
#add_user_to_group(requesting_user, user, group) ⇒ Symbol, Boolean
Adds a user to an authorization group.
-
#add_user_to_group!(user, group) ⇒ Boolean
Adds a user to an authorization group without validating the permissions of the requesting user.
-
#groups ⇒ Array<Symbol>
Returns a list of all authorization groups.
-
#groups_with_users ⇒ Hash
Returns a hash of authorization group names and the users in them.
-
#initialize(config) ⇒ Authorization
constructor
A new instance of Authorization.
-
#remove_user_from_group(requesting_user, user, group) ⇒ Symbol, Boolean
Removes a user from an authorization group.
-
#remove_user_from_group!(user, group) ⇒ Boolean
Removes a suer from an authorization group without validating the permissions of the requesting user.
-
#user_in_group?(user, group) ⇒ Boolean
Checks if a user is in an authorization group.
-
#user_is_admin?(user) ⇒ Boolean
Checks if a user is an administrator.
Constructor Details
#initialize(config) ⇒ Authorization
5 6 7 |
# File 'lib/lita/authorization.rb', line 5 def initialize(config) @config = config end |
Class Method Details
.add_user_to_group(*args) ⇒ Object
Will be removed in Lita 5.0 Use #add_user_to_group instead.
117 |
# File 'lib/lita/authorization.rb', line 117 define_deprecated_class_method :add_user_to_group |
.groups(*args) ⇒ Object
Will be removed in Lita 5.0 Use #groups instead.
121 |
# File 'lib/lita/authorization.rb', line 121 define_deprecated_class_method :groups |
.groups_with_users(*args) ⇒ Object
Will be removed in Lita 5.0 Use #groups_with_users instead.
122 |
# File 'lib/lita/authorization.rb', line 122 define_deprecated_class_method :groups_with_users |
.remove_user_from_group(*args) ⇒ Object
Will be removed in Lita 5.0 Use #remove_user_from_group instead.
118 |
# File 'lib/lita/authorization.rb', line 118 define_deprecated_class_method :remove_user_from_group |
.user_in_group?(*args) ⇒ Object
Will be removed in Lita 5.0 Use #user_in_group? instead.
119 |
# File 'lib/lita/authorization.rb', line 119 define_deprecated_class_method :user_in_group? |
.user_is_admin?(*args) ⇒ Object
Will be removed in Lita 5.0 Use #user_is_admin? instead.
120 |
# File 'lib/lita/authorization.rb', line 120 define_deprecated_class_method :user_is_admin? |
Instance Method Details
#add_user_to_group(requesting_user, user, group) ⇒ Symbol, Boolean
Adds a user to an authorization group.
16 17 18 19 |
# File 'lib/lita/authorization.rb', line 16 def add_user_to_group(requesting_user, user, group) return :unauthorized unless user_is_admin?(requesting_user) add_user_to_group!(user, group) end |
#add_user_to_group!(user, group) ⇒ Boolean
Adds a user to an authorization group without validating the permissions of the requesting user.
28 29 30 |
# File 'lib/lita/authorization.rb', line 28 def add_user_to_group!(user, group) redis.sadd(normalize_group(group), user.id) end |
#groups ⇒ Array<Symbol>
Returns a list of all authorization groups.
74 75 76 |
# File 'lib/lita/authorization.rb', line 74 def groups redis.keys("*").map(&:to_sym) end |
#groups_with_users ⇒ Hash
Returns a hash of authorization group names and the users in them.
80 81 82 83 84 85 86 87 |
# File 'lib/lita/authorization.rb', line 80 def groups_with_users groups.reduce({}) do |list, group| list[group] = redis.smembers(group).map do |user_id| User.find_by_id(user_id) end list end end |
#remove_user_from_group(requesting_user, user, group) ⇒ Symbol, Boolean
Removes a user from an authorization group.
39 40 41 42 |
# File 'lib/lita/authorization.rb', line 39 def remove_user_from_group(requesting_user, user, group) return :unauthorized unless user_is_admin?(requesting_user) remove_user_from_group!(user, group) end |
#remove_user_from_group!(user, group) ⇒ Boolean
Removes a suer from an authorization group without validating the permissions of the requesting user.
51 52 53 |
# File 'lib/lita/authorization.rb', line 51 def remove_user_from_group!(user, group) redis.srem(normalize_group(group), user.id) end |
#user_in_group?(user, group) ⇒ Boolean
Checks if a user is in an authorization group.
59 60 61 62 63 |
# File 'lib/lita/authorization.rb', line 59 def user_in_group?(user, group) group = normalize_group(group) return user_is_admin?(user) if group == "admins" redis.sismember(group, user.id) end |
#user_is_admin?(user) ⇒ Boolean
Checks if a user is an administrator.
68 69 70 |
# File 'lib/lita/authorization.rb', line 68 def user_is_admin?(user) Array(@config.robot.admins).include?(user.id) end |