class XNM::Telegram::User
This class is an extension of a Telegram
{Chat} object that represents a DM with a User
, and represents the user himself. This is because Telegram's User
ID is equivalent to the Chat
ID of the DM with the User
.
Attributes
A human readable name. Overrides {Chat#casual_name}
First name, always guaranteed to be set.
Last name, may not be set.
Permanent user state. Will be saved to disk. @todo Actually save to disk.
List of permissions this user has. This Array of Strings will be used to check against a executed command, to see if the User
has appropriate rights to run said command.
Note that the {Handler#permissions_list} will be used to expand this list, i.e. if the permissions list is: `{ 'admin' => ['basic_rights'] }` And the user has the 'admin' permission, he will also have basic_rights *without them being listed in {#permissions}*
Use {#has_permissions?} to check if a user has a certain permission.
Temporary state. Will be lost of restart, should be cleaned out and only used for intermediary work.
Username, without the @
Public Class Methods
Initialize a new user object.
Pass the handler used for this User
as well as the Hash containing Telegram's “User” Object.
XNM::Telegram::Chat::new
# File lib/xnm/telegram/User.rb, line 50 def initialize(handler, user_info) super(handler, user_info); @username = user_info[:username] @first_name = user_info[:first_name] @last_name = user_info[:last_name] @casual_name = user_info[:first_name] @permissions = user_info[:permissions] || [] @perm_state = user_info[:perm_state] || {} end
Public Instance Methods
# File lib/xnm/telegram/User.rb, line 63 def add_permissions(list) list = [list].flatten list.each do |perm| next if perm.is_a? Symbol next if perm.is_a? String raise ArgumentError, "Permission must be String or Symbol!" end @permissions = (@permissions + list).uniq end
Check whether a user has a given permission.
This function will check if the given permission is in the User's {#permissions}. It will also use the {Handler#permissions_list} to expand the user's permissions, i.e. if the permissions list is: `{ 'admin' => ['basic_rights'] }` And the user has the 'admin' permission, he will also have basic_rights *without them being listed in {#permissions}*
nil will return true.
@note This will always return true if the user has the :sudo
permission, use only for full admin access!
# File lib/xnm/telegram/User.rb, line 123 def has_permission?(target) return true if @permissions.include? :sudo return true if target.nil? unchecked = @permissions.dup checked = {} while perm = unchecked.pop next if checked[perm] return true if perm == target unchecked += @handler.permissions_list[perm] || [] checked[perm] = true end false end
Check if a user has all given permissions. Will run {#has_permission} against every permission in the list, returns false if any one permission is not met.
An empty list counts as true
# File lib/xnm/telegram/User.rb, line 96 def has_permissions?(*targets) targets = targets.flatten return true if targets.nil? return true if @permissions.include? :sudo targets.each do |perm| return false unless has_permission? perm end true end
# File lib/xnm/telegram/User.rb, line 77 def take_permissions(list) list = [list].flatten list.each do |perm| next if perm.is_a? Symbol next if perm.is_a? String raise ArgumentError, "Permission must be String or Symbol!" end @permissions -= list; end