Class: Etna::User
- Inherits:
-
Object
- Object
- Etna::User
- Defined in:
- lib/etna/user.rb
Constant Summary collapse
- ROLE_NAMES =
{ 'A' => :admin, 'E' => :editor, 'V' => :viewer }
- ROLE_MATCH =
{ admin: /[Aa]/, editor: /[Ee]/, viewer: /[Vv]/, restricted: /[AEV]/, }
Instance Attribute Summary collapse
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#first ⇒ Object
readonly
Returns the value of attribute first.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #can_edit?(project) ⇒ Boolean
-
#can_see_restricted?(project) ⇒ Boolean
superusers - administrators of the Administration group - cannot automatically see restricted data, they should be granted project-specific access.
- #can_view?(project) ⇒ Boolean
- #has_flag?(flag) ⇒ Boolean
- #has_roles(project, *roles) ⇒ Object
-
#initialize(params, token = nil) ⇒ User
constructor
A new instance of User.
- #is_admin?(project) ⇒ Boolean
- #is_superuser?(project = nil) ⇒ Boolean
- #name ⇒ Object
- #permissions ⇒ Object
- #projects ⇒ Object
Constructor Details
#initialize(params, token = nil) ⇒ User
Returns a new instance of User.
9 10 11 12 13 14 15 |
# File 'lib/etna/user.rb', line 9 def initialize params, token=nil @first, @last, @email, @encoded_permissions, encoded_flags = params.values_at(:first, :last, :email, :perm, :flags) @flags = encoded_flags&.split(/;/) || [] @token = token unless !token raise ArgumentError, "No email given!" unless @email end |
Instance Attribute Details
#email ⇒ Object (readonly)
Returns the value of attribute email.
17 18 19 |
# File 'lib/etna/user.rb', line 17 def email @email end |
#first ⇒ Object (readonly)
Returns the value of attribute first.
17 18 19 |
# File 'lib/etna/user.rb', line 17 def first @first end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
17 18 19 |
# File 'lib/etna/user.rb', line 17 def last @last end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
17 18 19 |
# File 'lib/etna/user.rb', line 17 def token @token end |
Instance Method Details
#can_edit?(project) ⇒ Boolean
65 66 67 |
# File 'lib/etna/user.rb', line 65 def can_edit? project is_superuser? || has_roles(project, :admin, :editor) end |
#can_see_restricted?(project) ⇒ Boolean
superusers - administrators of the Administration group - cannot automatically see restricted data, they should be granted project-specific access.
76 77 78 79 |
# File 'lib/etna/user.rb', line 76 def can_see_restricted? project perm = [project.to_s] perm && perm[:restricted] end |
#can_view?(project) ⇒ Boolean
69 70 71 |
# File 'lib/etna/user.rb', line 69 def can_view? project is_superuser? || has_roles(project, :admin, :editor, :viewer) end |
#has_flag?(flag) ⇒ Boolean
35 36 37 |
# File 'lib/etna/user.rb', line 35 def has_flag?(flag) @flags.include?(flag) end |
#has_roles(project, *roles) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/etna/user.rb', line 53 def has_roles(project, *roles) perm = [project.to_s] return false unless perm return roles.map(&:to_sym).include?(perm[:role]) end |
#is_admin?(project) ⇒ Boolean
81 82 83 |
# File 'lib/etna/user.rb', line 81 def is_admin? project is_superuser? || has_roles(project, :admin) end |
#is_superuser?(project = nil) ⇒ Boolean
61 62 63 |
# File 'lib/etna/user.rb', line 61 def is_superuser? project=nil has_roles(:administration, :admin) end |
#name ⇒ Object
39 40 41 |
# File 'lib/etna/user.rb', line 39 def name "#{first} #{last}" end |
#permissions ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/etna/user.rb', line 19 def @permissions ||= @encoded_permissions.split(/\;/).map do |roles| role, projects = roles.split(/:/) projects.split(/\,/).reduce([]) do |perms,project_name| perms.push([ project_name, { role: ROLE_NAMES[role.upcase], restricted: role == role.upcase } ]) end end.inject([],:+).to_h end |
#projects ⇒ Object
43 44 45 |
# File 'lib/etna/user.rb', line 43 def projects .keys end |