Module: Cinch::Extensions::Authentication
- Included in:
- Plugins::UserList, Plugins::UserLogin
- Defined in:
- lib/cinch/extensions/authentication.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#authenticated?(m, levels = nil) ⇒ Boolean
Public: Checks if the user is authorized to run the command.
-
#channel_status_strategy(m, level) ⇒ Object
Internal: Checks if the user is an operator on the channel.
-
#current_user(m) ⇒ Object
Public: Returns the nickname of the currently logged in user.
-
#list_strategy(m, levels) ⇒ Object
Internal: Checks if the user sending the message is on the user list.
-
#login_strategy(m, levels) ⇒ Object
Internal: Checks if the user sending the message has logged in.
Class Method Details
.included(base) ⇒ Object
13 14 15 |
# File 'lib/cinch/extensions/authentication.rb', line 13 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#authenticated?(m, levels = nil) ⇒ Boolean
Public: Checks if the user is authorized to run the command.
m - The Cinch::Message. levels - The level(s) of authentication Symbol(s) the user must have
(default: nil).
Examples
# The :channel_status strategy
authenticated? m, :h # => true for :q, :a, :o and :h
authenticated? m, :o # => true for :q, :a and :o
# The :user_list and :user_login strategy
authenticated? m, [:admins, :users]
authenticated? m, :admins
Returns a Boolean.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cinch/extensions/authentication.rb', line 34 def authenticated?(m, levels = nil) strategy = config[:authentication_strategy] || bot.config.authentication.strategy levels = levels || config[:authentication_level] || bot.config.authentication.level case strategy when :channel_status then return channel_status_strategy m, levels when :list then return list_strategy m, levels when :login then return login_strategy m, levels end raise StandardError, 'You have not configured an authentication ' + 'strategy.' end |
#channel_status_strategy(m, level) ⇒ Object
Internal: Checks if the user is an operator on the channel.
m - The Cinch::Message. level - The level Symbol.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cinch/extensions/authentication.rb', line 54 def channel_status_strategy(m, level) if config.has_key? :authentication_channel channel = Channel channel[:authentication_channel] elsif bot.config.authentication.channel channel = Channel bot.config.authentication.channel else channel = m.channel end if level.nil? raise StandardError, "You haven't configured an authentication level." end bot.loggers.debug level.inspect user_modes = channel.users[m.user] modes = { q: 'founder', a: 'admin', o: 'operator', h: 'half-operator', v: 'voice' } modes.keys.take(modes.keys.find_index(level) + 1).each do |mode| return true if user_modes.include? mode.to_s end m.user.notice "This command requires at least #{modes[level]} status " + "on #{channel}." return false rescue => e m.user.notice 'Something went wrong.' raise end |
#current_user(m) ⇒ Object
Public: Returns the nickname of the currently logged in user.
Returns a String. Returns nil when a user is not logged in.
146 147 148 |
# File 'lib/cinch/extensions/authentication.rb', line 146 def current_user(m) bot.config.authentication.logged_in[m.user] end |
#list_strategy(m, levels) ⇒ Object
Internal: Checks if the user sending the message is on the user list.
m - The Cinch::Message. levels - The level Symbol(s).
Returns a Boolean.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cinch/extensions/authentication.rb', line 92 def list_strategy(m, levels) m.user.refresh unless m.user.authed? m.user.notice "This command requires you to be authenticated." return false end user_list = Array(levels).each_with_object [] do |level, list| list.concat(config[level] || bot.config.authentication.send(level)) end if user_list.nil? bot.loggers.debug "You have not configured any user lists." end unless user_list.include? m.user.authname m.user.notice "You are not authorized to run this command." return false end return true end |
#login_strategy(m, levels) ⇒ Object
Internal: Checks if the user sending the message has logged in.
m- The Cinch::Message. levels - The level Symbol(s).
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/cinch/extensions/authentication.rb', line 120 def login_strategy(m, levels) if current_user(m).nil? m.user.notice "You are not authorized to run this command. Please " + "log in using !login [<username>] <password>." return false end levels = Array(levels) levels.each do |level| level_lambda = config[level] || bot.config.authentication.send(level) return true if level_lambda.call current_user(m) end # The previous check will fail if no level is given. return true if levels.nil? m.user.notice 'You are not authorized to run this command.' return false end |