Class: Lita::Authorization

Inherits:
Object
  • Object
show all
Defined in:
lib/lita/authorization.rb

Overview

Methods for querying and manipulating authorization groups.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Authorization

Returns a new instance of Authorization

Parameters:



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

Deprecated.

Will be removed in Lita 5.0 Use #add_user_to_group instead.

See Also:



117
# File 'lib/lita/authorization.rb', line 117

define_deprecated_class_method :add_user_to_group

.groups(*args) ⇒ Object

Deprecated.

Will be removed in Lita 5.0 Use #groups instead.

See Also:



121
# File 'lib/lita/authorization.rb', line 121

define_deprecated_class_method :groups

.groups_with_users(*args) ⇒ Object

Deprecated.

Will be removed in Lita 5.0 Use #groups_with_users instead.

See Also:



122
# File 'lib/lita/authorization.rb', line 122

define_deprecated_class_method :groups_with_users

.remove_user_from_group(*args) ⇒ Object

Deprecated.

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

Deprecated.

Will be removed in Lita 5.0 Use #user_in_group? instead.

See Also:



119
# File 'lib/lita/authorization.rb', line 119

define_deprecated_class_method :user_in_group?

.user_is_admin?(*args) ⇒ Object

Deprecated.

Will be removed in Lita 5.0 Use #user_is_admin? instead.

See Also:



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.

Parameters:

  • requesting_user (Lita::User)

    The user who sent the command.

  • user (Lita::User)

    The user to add to the group.

  • group (Symbol, String)

    The name of the group.

Returns:

  • (Symbol)

    :unauthorized if the requesting user is not authorized.

  • (Boolean)

    true if the user was added. false if the user was already in the 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.

Parameters:

  • user (Lita::User)

    The user to add to the group.

  • group (Symbol, String)

    The name of the group.

Returns:

  • (Boolean)

    true if the user was added. false if the user was already in the group.

Since:

  • 4.0.0



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

#groupsArray<Symbol>

Returns a list of all authorization groups.

Returns:

  • (Array<Symbol>)

    The names of all authorization groups.



74
75
76
# File 'lib/lita/authorization.rb', line 74

def groups
  redis.keys("*").map(&:to_sym)
end

#groups_with_usersHash

Returns a hash of authorization group names and the users in them.

Returns:

  • (Hash)

    A map of Symbol group names to Lita::User objects.



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.

Parameters:

  • requesting_user (Lita::User)

    The user who sent the command.

  • user (Lita::User)

    The user to remove from the group.

  • group (Symbol, String)

    The name of the group.

Returns:

  • (Symbol)

    :unauthorized if the requesting user is not authorized.

  • (Boolean)

    true if the user was removed. false if the user was not in the 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.

Parameters:

  • user (Lita::User)

    The user to remove from the group.

  • group (Symbol, String)

    The name of the group.

Returns:

  • (Boolean)

    true if the user was removed. false if the user was not in the group.

Since:

  • 4.0.0



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.

Parameters:

  • user (Lita::User)

    The user.

  • group (Symbol, String)

    The name of the group.

Returns:

  • (Boolean)

    Whether or not the user is in the 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.

Parameters:

Returns:

  • (Boolean)

    Whether or not the 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