lita 3.3.1 → 4.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/.travis.yml +3 -0
- data/lib/lita.rb +45 -97
- data/lib/lita/adapter.rb +38 -17
- data/lib/lita/adapters/shell.rb +5 -3
- data/lib/lita/authorization.rb +109 -60
- data/lib/lita/builder.rb +38 -0
- data/lib/lita/callback.rb +37 -0
- data/lib/lita/cli.rb +2 -0
- data/lib/lita/config.rb +1 -18
- data/lib/lita/configurable.rb +29 -0
- data/lib/lita/configuration.rb +179 -0
- data/lib/lita/configuration_validator.rb +66 -0
- data/lib/lita/daemon.rb +4 -11
- data/lib/lita/default_configuration.rb +146 -0
- data/lib/lita/errors.rb +9 -0
- data/lib/lita/handler.rb +5 -264
- data/lib/lita/handler/chat_router.rb +130 -0
- data/lib/lita/handler/common.rb +114 -0
- data/lib/lita/handler/event_router.rb +77 -0
- data/lib/lita/handler/http_router.rb +26 -0
- data/lib/lita/handlers/authorization.rb +13 -18
- data/lib/lita/handlers/deprecation_check.rb +24 -0
- data/lib/lita/handlers/help.rb +5 -3
- data/lib/lita/handlers/info.rb +2 -2
- data/lib/lita/http_callback.rb +29 -0
- data/lib/lita/http_route.rb +41 -26
- data/lib/lita/namespace.rb +23 -0
- data/lib/lita/rack_app.rb +29 -2
- data/lib/lita/registry.rb +133 -0
- data/lib/lita/robot.rb +58 -20
- data/lib/lita/route_validator.rb +12 -4
- data/lib/lita/rspec.rb +23 -14
- data/lib/lita/rspec/handler.rb +93 -23
- data/lib/lita/rspec/matchers/chat_route_matcher.rb +48 -0
- data/lib/lita/rspec/matchers/deprecated.rb +36 -0
- data/lib/lita/rspec/matchers/event_route_matcher.rb +27 -0
- data/lib/lita/rspec/matchers/http_route_matcher.rb +18 -60
- data/lib/lita/user.rb +0 -6
- data/lib/lita/util.rb +1 -8
- data/lib/lita/version.rb +1 -1
- data/lita.gemspec +1 -0
- data/spec/lita/adapter_spec.rb +25 -7
- data/spec/lita/adapters/shell_spec.rb +24 -4
- data/spec/lita/authorization_spec.rb +57 -38
- data/spec/lita/builder_spec.rb +39 -0
- data/spec/lita/config_spec.rb +0 -24
- data/spec/lita/configuration_spec.rb +222 -0
- data/spec/lita/configuration_validator_spec.rb +112 -0
- data/spec/lita/daemon_spec.rb +2 -2
- data/spec/lita/default_configuration_spec.rb +254 -0
- data/spec/lita/handler/chat_router_spec.rb +192 -0
- data/spec/lita/handler/common_spec.rb +272 -0
- data/spec/lita/handler/event_router_spec.rb +54 -0
- data/spec/lita/handler/http_router_spec.rb +106 -0
- data/spec/lita/handler_spec.rb +20 -291
- data/spec/lita/handlers/authorization_spec.rb +9 -11
- data/spec/lita/handlers/deprecation_check_spec.rb +21 -0
- data/spec/lita/handlers/help_spec.rb +31 -9
- data/spec/lita/handlers/info_spec.rb +2 -2
- data/spec/lita/handlers/room_spec.rb +5 -3
- data/spec/lita/robot_spec.rb +30 -11
- data/spec/lita/rspec_spec.rb +71 -31
- data/spec/lita/user_spec.rb +2 -2
- data/spec/lita_spec.rb +62 -4
- data/spec/spec_helper.rb +7 -0
- data/templates/locales/en.yml +56 -4
- data/templates/plugin/gemspec.tt +1 -0
- data/templates/plugin/spec/spec_helper.tt +4 -0
- metadata +54 -8
- data/lib/lita/rspec/matchers/event_subscription_matcher.rb +0 -67
- data/lib/lita/rspec/matchers/route_matcher.rb +0 -69
- data/spec/lita/rack_app_spec.rb +0 -92
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6aba9ae845dbe0d8fcbeedca12735d4cde375b44
|
4
|
+
data.tar.gz: 7afcedf903295c434f39390884c7f7fe8465c181
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 174c6be0c949ee3fd83753348d65c862b6313b5f77164b6c6b928946524e1913adbd07c886f352ce9daedc19e8243a5016bb623e352701e8bfdc369ac1f9aa26
|
7
|
+
data.tar.gz: ce15b54ce11d9efcb90b6b6db82a246625d6b38154db75fdac127629ef1a60892e60e0a607d31d1853a539538d781285d89fbd22557ac1fef8d5dc67e623b823
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/lib/lita.rb
CHANGED
@@ -14,6 +14,8 @@ require "puma"
|
|
14
14
|
require "rack"
|
15
15
|
require "redis-namespace"
|
16
16
|
|
17
|
+
require_relative "lita/registry"
|
18
|
+
|
17
19
|
# The main namespace for Lita. Provides a global registry of adapters and
|
18
20
|
# handlers, as well as global configuration, logger, and Redis store.
|
19
21
|
module Lita
|
@@ -21,43 +23,24 @@ module Lita
|
|
21
23
|
REDIS_NAMESPACE = "lita"
|
22
24
|
|
23
25
|
class << self
|
24
|
-
|
25
|
-
# @return [Hash] A map of adapter keys to adapter classes.
|
26
|
-
def adapters
|
27
|
-
@adapters ||= {}
|
28
|
-
end
|
26
|
+
include Registry::Mixins
|
29
27
|
|
30
|
-
#
|
31
|
-
# @return [
|
32
|
-
|
33
|
-
|
34
|
-
|
28
|
+
# A special mode to ensure that tests written for Lita 3 plugins continue to work.
|
29
|
+
# @return [Boolean] Whether or not version 3 compatibility mode is active.
|
30
|
+
# @since 4.0.0
|
31
|
+
attr_accessor :version_3_compatibility_mode
|
32
|
+
alias_method :version_3_compatibility_mode?, :version_3_compatibility_mode
|
35
33
|
|
36
|
-
#
|
37
|
-
#
|
38
|
-
# @
|
39
|
-
|
40
|
-
|
41
|
-
yield config
|
42
|
-
end
|
43
|
-
|
44
|
-
# The global registry of handlers.
|
45
|
-
# @return [Set] The set of handlers.
|
46
|
-
def handlers
|
47
|
-
@handlers ||= Set.new
|
48
|
-
end
|
49
|
-
|
50
|
-
# The global registry of hook handler objects.
|
51
|
-
# @return [Hash] A hash mapping hook names to sets of objects that handle them.
|
52
|
-
# @since 3.2.0
|
53
|
-
def hooks
|
54
|
-
@hooks ||= Hash.new { |h, k| h[k] = Set.new }
|
55
|
-
end
|
34
|
+
# A mode that makes minor changes to the Lita runtime to improve testability.
|
35
|
+
# @return [Boolean] Whether or not test mode is active.
|
36
|
+
# @since 4.0.0
|
37
|
+
attr_accessor :test_mode
|
38
|
+
alias_method :test_mode?, :test_mode
|
56
39
|
|
57
40
|
# The global Logger object.
|
58
41
|
# @return [::Logger] The global Logger object.
|
59
42
|
def logger
|
60
|
-
@logger ||= Logger.get_logger(
|
43
|
+
@logger ||= Logger.get_logger(config.robot.log_level)
|
61
44
|
end
|
62
45
|
|
63
46
|
# The root Redis object.
|
@@ -65,79 +48,30 @@ module Lita
|
|
65
48
|
def redis
|
66
49
|
@redis ||= begin
|
67
50
|
redis = Redis.new(config.redis)
|
68
|
-
Redis::Namespace.new(REDIS_NAMESPACE, redis: redis)
|
51
|
+
Redis::Namespace.new(REDIS_NAMESPACE, redis: redis).tap do |client|
|
52
|
+
begin
|
53
|
+
client.ping
|
54
|
+
rescue Redis::BaseError => e
|
55
|
+
Lita.logger.fatal I18n.t(
|
56
|
+
"lita.redis.exception",
|
57
|
+
message: e.message,
|
58
|
+
backtrace: e.backtrace.join("\n")
|
59
|
+
)
|
60
|
+
abort
|
61
|
+
end
|
62
|
+
end
|
69
63
|
end
|
70
64
|
end
|
71
65
|
|
72
|
-
# Adds an adapter to the global registry under the provided key.
|
73
|
-
# @param key [String, Symbol] The key that identifies the adapter.
|
74
|
-
# @param adapter [Lita::Adapter] The adapter class.
|
75
|
-
# @return [void]
|
76
|
-
def register_adapter(key, adapter)
|
77
|
-
adapters[key.to_sym] = adapter
|
78
|
-
end
|
79
|
-
|
80
|
-
# Adds a handler to the global registry.
|
81
|
-
# @param handler [Lita::Handler] The handler class.
|
82
|
-
# @return [void]
|
83
|
-
def register_handler(handler)
|
84
|
-
handlers << handler
|
85
|
-
end
|
86
|
-
|
87
|
-
# Adds a hook handler object to the global registry for the given hook.
|
88
|
-
# @return [void]
|
89
|
-
# @since 3.2.0
|
90
|
-
def register_hook(name, hook)
|
91
|
-
hooks[name.to_s.downcase.strip.to_sym] << hook
|
92
|
-
end
|
93
|
-
|
94
|
-
# Clears the global configuration object and the global adapter, handler, and hook registries.
|
95
|
-
# @return [void]
|
96
|
-
# @since 3.2.0
|
97
|
-
def reset
|
98
|
-
reset_adapters
|
99
|
-
reset_config
|
100
|
-
reset_handlers
|
101
|
-
reset_hooks
|
102
|
-
end
|
103
|
-
|
104
|
-
# Resets the global adapter registry, removing all registered adapters.
|
105
|
-
# @return [void]
|
106
|
-
# @since 3.2.0
|
107
|
-
def reset_adapters
|
108
|
-
@adapters = nil
|
109
|
-
end
|
110
|
-
|
111
|
-
# Resets the global configuration object. The next call to {Lita.config}
|
112
|
-
# will create a fresh config object.
|
113
|
-
# @return [void]
|
114
|
-
def reset_config
|
115
|
-
@config = nil
|
116
|
-
end
|
117
|
-
alias_method :clear_config, :reset_config
|
118
|
-
|
119
|
-
# Resets the global handler registry, removing all registered handlers.
|
120
|
-
# @return [void]
|
121
|
-
# @since 3.2.0
|
122
|
-
def reset_handlers
|
123
|
-
@handlers = nil
|
124
|
-
end
|
125
|
-
|
126
|
-
# Resets the global hooks registry, removing all registered hook handlers.
|
127
|
-
# @return [void]
|
128
|
-
# @since 3.2.0
|
129
|
-
def reset_hooks
|
130
|
-
@hooks = nil
|
131
|
-
end
|
132
|
-
|
133
66
|
# Loads user configuration and starts the robot.
|
134
67
|
# @param config_path [String] The path to the user configuration file.
|
135
68
|
# @return [void]
|
136
69
|
def run(config_path = nil)
|
137
70
|
hooks[:before_run].each { |hook| hook.call(config_path: config_path) }
|
138
|
-
|
139
|
-
|
140
|
-
self.
|
71
|
+
Configuration.load_user_config(config_path)
|
72
|
+
Configuration.freeze_config(config)
|
73
|
+
ConfigurationValidator.new(self).call
|
74
|
+
self.locale = config.robot.locale
|
141
75
|
Robot.new.run
|
142
76
|
end
|
143
77
|
end
|
@@ -145,23 +79,37 @@ end
|
|
145
79
|
|
146
80
|
require_relative "lita/version"
|
147
81
|
require_relative "lita/common"
|
82
|
+
require_relative "lita/errors"
|
148
83
|
require_relative "lita/config"
|
84
|
+
require_relative "lita/configuration"
|
85
|
+
require_relative "lita/default_configuration"
|
86
|
+
require_relative "lita/configuration_validator"
|
149
87
|
require_relative "lita/util"
|
150
88
|
require_relative "lita/logger"
|
89
|
+
require_relative "lita/callback"
|
90
|
+
require_relative "lita/configurable"
|
91
|
+
require_relative "lita/namespace"
|
92
|
+
require_relative "lita/handler/common"
|
93
|
+
require_relative "lita/handler/chat_router"
|
94
|
+
require_relative "lita/handler/http_router"
|
95
|
+
require_relative "lita/handler/event_router"
|
96
|
+
require_relative "lita/handler"
|
151
97
|
require_relative "lita/user"
|
152
98
|
require_relative "lita/source"
|
153
99
|
require_relative "lita/authorization"
|
154
100
|
require_relative "lita/message"
|
155
101
|
require_relative "lita/response"
|
102
|
+
require_relative "lita/http_callback"
|
156
103
|
require_relative "lita/http_route"
|
157
104
|
require_relative "lita/rack_app"
|
158
105
|
require_relative "lita/timer"
|
159
106
|
require_relative "lita/robot"
|
160
107
|
require_relative "lita/adapter"
|
161
108
|
require_relative "lita/adapters/shell"
|
162
|
-
require_relative "lita/
|
109
|
+
require_relative "lita/builder"
|
163
110
|
require_relative "lita/route_validator"
|
164
111
|
require_relative "lita/handlers/authorization"
|
165
112
|
require_relative "lita/handlers/help"
|
166
113
|
require_relative "lita/handlers/info"
|
167
114
|
require_relative "lita/handlers/room"
|
115
|
+
require_relative "lita/handlers/deprecation_check"
|
data/lib/lita/adapter.rb
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
module Lita
|
2
2
|
# Adapters are the glue between Lita's API and a chat service.
|
3
3
|
class Adapter
|
4
|
+
extend Namespace
|
5
|
+
extend Configurable
|
6
|
+
|
4
7
|
# The instance of {Lita::Robot}.
|
5
8
|
# @return [Lita::Robot]
|
6
9
|
attr_reader :robot
|
7
10
|
|
8
11
|
class << self
|
12
|
+
# @!attribute [r] required_configs
|
9
13
|
# A list of configuration keys that are required for the adapter to boot.
|
10
14
|
# @return [Array]
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
# @return [String] The adapter's namespace.
|
16
|
-
# @raise [RuntimeError] If +self.name+ is not defined.
|
17
|
-
def namespace
|
18
|
-
if name
|
19
|
-
Util.underscore(name.split("::").last)
|
20
|
-
else
|
21
|
-
raise I18n.t("lita.adapter.name_required")
|
22
|
-
end
|
15
|
+
# @deprecated Will be removed in Lita 5.0. Use {Lita::Adapter#configuration} instead.
|
16
|
+
def required_configs
|
17
|
+
Lita.logger.warn(I18n.t("lita.adapter.required_configs_deprecated"))
|
18
|
+
@required_configs
|
23
19
|
end
|
24
20
|
|
25
21
|
# Defines configuration keys that are requried for the adapter to boot.
|
26
22
|
# @param keys [String, Symbol] The required keys.
|
27
23
|
# @return [void]
|
24
|
+
# @deprecated Will be removed in Lita 5.0. Use {Lita::Adapter#config} instead.
|
28
25
|
def require_config(*keys)
|
29
26
|
@required_configs ||= []
|
30
27
|
@required_configs.concat(keys.flatten.map(&:to_sym))
|
@@ -48,6 +45,13 @@ module Lita
|
|
48
45
|
@robot = robot
|
49
46
|
ensure_required_configs
|
50
47
|
end
|
48
|
+
#
|
49
|
+
# The handler's config object.
|
50
|
+
# @return [Lita::Configuration] The adapter's configuration object.
|
51
|
+
# @since 4.0.0
|
52
|
+
def config
|
53
|
+
robot.config.adapters.public_send(self.class.namespace)
|
54
|
+
end
|
51
55
|
|
52
56
|
# @!method join
|
53
57
|
# Joins the room with the specified ID.
|
@@ -113,21 +117,38 @@ module Lita
|
|
113
117
|
|
114
118
|
private
|
115
119
|
|
120
|
+
# Returns the object used for the adapter's config.
|
121
|
+
def adapter_config
|
122
|
+
if Lita.version_3_compatibility_mode?
|
123
|
+
Lita.config.adapter
|
124
|
+
else
|
125
|
+
robot.config.adapter
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
116
129
|
# Logs a fatal message and aborts if a required config key is not set.
|
117
130
|
def ensure_required_configs
|
118
|
-
required_configs = self.class.required_configs
|
119
131
|
return if required_configs.nil?
|
120
132
|
|
121
|
-
|
133
|
+
Lita.logger.warn(I18n.t("lita.adapter.require_config_deprecated"))
|
122
134
|
|
123
|
-
|
124
|
-
missing_keys << key unless Lita.config.adapter[key]
|
125
|
-
end
|
135
|
+
missing_keys = missing_config_keys
|
126
136
|
|
127
137
|
unless missing_keys.empty?
|
128
138
|
Lita.logger.fatal(I18n.t("lita.adapter.missing_configs", configs: missing_keys.join(", ")))
|
129
139
|
abort
|
130
140
|
end
|
131
141
|
end
|
142
|
+
|
143
|
+
# Finds all missing config keys.
|
144
|
+
def missing_config_keys
|
145
|
+
required_configs.select do |key|
|
146
|
+
key unless adapter_config[key]
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def required_configs
|
151
|
+
self.class.instance_variable_get(:@required_configs)
|
152
|
+
end
|
132
153
|
end
|
133
154
|
end
|
data/lib/lita/adapters/shell.rb
CHANGED
@@ -3,6 +3,8 @@ module Lita
|
|
3
3
|
module Adapters
|
4
4
|
# An adapter that runs Lita in a UNIX shell.
|
5
5
|
class Shell < Adapter
|
6
|
+
config :private_chat, default: false
|
7
|
+
|
6
8
|
# Creates a "Shell User" and then loops a prompt and input, passing the
|
7
9
|
# incoming messages to the robot.
|
8
10
|
# @return [void]
|
@@ -16,7 +18,7 @@ module Lita
|
|
16
18
|
end
|
17
19
|
|
18
20
|
# Outputs outgoing messages to the shell.
|
19
|
-
# @param
|
21
|
+
# @param _target [Lita::Source] Unused, since there is only one user in the
|
20
22
|
# shell environment.
|
21
23
|
# @param strings [Array<String>] An array of strings to output.
|
22
24
|
# @return [void]
|
@@ -39,7 +41,7 @@ module Lita
|
|
39
41
|
|
40
42
|
def build_message(input, source)
|
41
43
|
message = Message.new(robot, input, source)
|
42
|
-
message.command! if
|
44
|
+
message.command! if robot.config.adapters.shell.private_chat
|
43
45
|
message
|
44
46
|
end
|
45
47
|
|
@@ -57,7 +59,7 @@ module Lita
|
|
57
59
|
input = Readline.readline("#{robot.name} > ", true)
|
58
60
|
# Input read via rb-readline will always be encoded as US-ASCII.
|
59
61
|
# @see https://github.com/luislavena/rb-readline/blob/master/lib/readline.rb#L1
|
60
|
-
input.force_encoding(Encoding.default_external)
|
62
|
+
input.force_encoding(Encoding.default_external) if input
|
61
63
|
end
|
62
64
|
|
63
65
|
def run_loop
|
data/lib/lita/authorization.rb
CHANGED
@@ -1,76 +1,125 @@
|
|
1
1
|
module Lita
|
2
2
|
# Methods for querying and manipulating authorization groups.
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
# @param group [Symbol, String] The name of the group.
|
9
|
-
# @return [Symbol] :unauthorized if the requesting user is not authorized.
|
10
|
-
# @return [Boolean] true if the user was added. false if the user was
|
11
|
-
# already in the group.
|
12
|
-
def add_user_to_group(requesting_user, user, group)
|
13
|
-
return :unauthorized unless user_is_admin?(requesting_user)
|
14
|
-
redis.sadd(normalize_group(group), user.id)
|
15
|
-
end
|
3
|
+
class Authorization
|
4
|
+
# @param config [Object] The finalized configuration object for the currently running robot.
|
5
|
+
def initialize(config)
|
6
|
+
@config = config
|
7
|
+
end
|
16
8
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
9
|
+
# Adds a user to an authorization group.
|
10
|
+
# @param requesting_user [Lita::User] The user who sent the command.
|
11
|
+
# @param user [Lita::User] The user to add to the group.
|
12
|
+
# @param group [Symbol, String] The name of the group.
|
13
|
+
# @return [Symbol] :unauthorized if the requesting user is not authorized.
|
14
|
+
# @return [Boolean] true if the user was added. false if the user was
|
15
|
+
# already in the group.
|
16
|
+
def add_user_to_group(requesting_user, user, group)
|
17
|
+
return :unauthorized unless user_is_admin?(requesting_user)
|
18
|
+
add_user_to_group!(user, group)
|
19
|
+
end
|
28
20
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
21
|
+
# Adds a user to an authorization group without validating the permissions
|
22
|
+
# of the requesting user.
|
23
|
+
# @param user [Lita::User] The user to add to the group.
|
24
|
+
# @param group [Symbol, String] The name of the group.
|
25
|
+
# @return [Boolean] true if the user was added. false if the user was
|
26
|
+
# already in the group.
|
27
|
+
# @since 4.0.0
|
28
|
+
def add_user_to_group!(user, group)
|
29
|
+
redis.sadd(normalize_group(group), user.id)
|
30
|
+
end
|
38
31
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
32
|
+
# Removes a user from an authorization group.
|
33
|
+
# @param requesting_user [Lita::User] The user who sent the command.
|
34
|
+
# @param user [Lita::User] The user to remove from the group.
|
35
|
+
# @param group [Symbol, String] The name of the group.
|
36
|
+
# @return [Symbol] :unauthorized if the requesting user is not authorized.
|
37
|
+
# @return [Boolean] true if the user was removed. false if the user was
|
38
|
+
# not in the group.
|
39
|
+
def remove_user_from_group(requesting_user, user, group)
|
40
|
+
return :unauthorized unless user_is_admin?(requesting_user)
|
41
|
+
remove_user_from_group!(user, group)
|
42
|
+
end
|
45
43
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
44
|
+
# Removes a suer from an authorization group without validating the
|
45
|
+
# permissions of the requesting user.
|
46
|
+
# @param user [Lita::User] The user to remove from the group.
|
47
|
+
# @param group [Symbol, String] The name of the group.
|
48
|
+
# @return [Boolean] true if the user was removed. false if the user was
|
49
|
+
# not in the group.
|
50
|
+
# @since 4.0.0
|
51
|
+
def remove_user_from_group!(user, group)
|
52
|
+
redis.srem(normalize_group(group), user.id)
|
53
|
+
end
|
51
54
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
# Checks if a user is in an authorization group.
|
56
|
+
# @param user [Lita::User] The user.
|
57
|
+
# @param group [Symbol, String] The name of the group.
|
58
|
+
# @return [Boolean] Whether or not the user is in the group.
|
59
|
+
def user_in_group?(user, group)
|
60
|
+
group = normalize_group(group)
|
61
|
+
return user_is_admin?(user) if group == "admins"
|
62
|
+
redis.sismember(group, user.id)
|
63
|
+
end
|
64
|
+
|
65
|
+
# Checks if a user is an administrator.
|
66
|
+
# @param user [Lita::User] The user.
|
67
|
+
# @return [Boolean] Whether or not the user is an administrator.
|
68
|
+
def user_is_admin?(user)
|
69
|
+
Array(@config.robot.admins).include?(user.id)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Returns a list of all authorization groups.
|
73
|
+
# @return [Array<Symbol>] The names of all authorization groups.
|
74
|
+
def groups
|
75
|
+
redis.keys("*").map(&:to_sym)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Returns a hash of authorization group names and the users in them.
|
79
|
+
# @return [Hash] A map of +Symbol+ group names to +Lita::User+ objects.
|
80
|
+
def groups_with_users
|
81
|
+
groups.reduce({}) do |list, group|
|
82
|
+
list[group] = redis.smembers(group).map do |user_id|
|
83
|
+
User.find_by_id(user_id)
|
60
84
|
end
|
85
|
+
list
|
61
86
|
end
|
87
|
+
end
|
62
88
|
|
63
|
-
|
89
|
+
private
|
64
90
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
91
|
+
# Ensures that group names are stored consistently in Redis.
|
92
|
+
def normalize_group(group)
|
93
|
+
group.to_s.downcase.strip
|
94
|
+
end
|
69
95
|
|
70
|
-
|
71
|
-
|
72
|
-
|
96
|
+
# A Redis::Namespace for authorization data.
|
97
|
+
def redis
|
98
|
+
@redis ||= Redis::Namespace.new("auth", redis: Lita.redis)
|
99
|
+
end
|
100
|
+
|
101
|
+
class << self
|
102
|
+
class << self
|
103
|
+
private
|
104
|
+
|
105
|
+
# @!macro define_deprecated_class_method
|
106
|
+
# @method $1(*args)
|
107
|
+
# @see #$1
|
108
|
+
# @deprecated Will be removed in Lita 5.0 Use #$1 instead.
|
109
|
+
def define_deprecated_class_method(deprecated_method)
|
110
|
+
define_method(deprecated_method) do |*args|
|
111
|
+
Lita.logger.warn(I18n.t("lita.auth.class_method_deprecated", method: deprecated_method))
|
112
|
+
new(Lita.config).public_send(deprecated_method, *args)
|
113
|
+
end
|
114
|
+
end
|
73
115
|
end
|
116
|
+
|
117
|
+
define_deprecated_class_method :add_user_to_group
|
118
|
+
define_deprecated_class_method :remove_user_from_group
|
119
|
+
define_deprecated_class_method :user_in_group?
|
120
|
+
define_deprecated_class_method :user_is_admin?
|
121
|
+
define_deprecated_class_method :groups
|
122
|
+
define_deprecated_class_method :groups_with_users
|
74
123
|
end
|
75
124
|
end
|
76
125
|
end
|