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.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #active?(project = nil) ⇒ Boolean
- #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_any_role?(project, *roles) ⇒ Boolean
- #has_flag?(flag) ⇒ Boolean
-
#initialize(params, token = nil) ⇒ User
constructor
A new instance of User.
- #is_admin?(project) ⇒ Boolean
- #is_supereditor?(project = nil) ⇒ Boolean
- #is_superuser?(project = nil) ⇒ Boolean
- #is_superviewer?(project = nil) ⇒ Boolean
- #permissions ⇒ Object
- #projects ⇒ Object
- #task? ⇒ Boolean
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 @name, @email, @encoded_permissions, encoded_flags, @task = params.values_at(:name, :email, :perm, :flags, :task) @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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/etna/user.rb', line 17 def name @name 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
#active?(project = nil) ⇒ Boolean
93 94 95 |
# File 'lib/etna/user.rb', line 93 def active? project=nil .keys.length > 0 end |
#can_edit?(project) ⇒ Boolean
73 74 75 |
# File 'lib/etna/user.rb', line 73 def can_edit? project is_supereditor? || has_any_role?(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.
84 85 86 87 |
# File 'lib/etna/user.rb', line 84 def can_see_restricted? project perm = [project.to_s] perm && perm[:restricted] end |
#can_view?(project) ⇒ Boolean
77 78 79 |
# File 'lib/etna/user.rb', line 77 def can_view? project is_superviewer? || has_any_role?(project, :admin, :editor, :viewer) end |
#has_any_role?(project, *roles) ⇒ Boolean
53 54 55 56 57 58 59 |
# File 'lib/etna/user.rb', line 53 def has_any_role?(project, *roles) perm = [project.to_s] return false unless perm return roles.map(&:to_sym).include?(perm[:role]) end |
#has_flag?(flag) ⇒ Boolean
39 40 41 |
# File 'lib/etna/user.rb', line 39 def has_flag?(flag) @flags.include?(flag) end |
#is_admin?(project) ⇒ Boolean
89 90 91 |
# File 'lib/etna/user.rb', line 89 def is_admin? project is_superuser? || has_any_role?(project, :admin) end |
#is_supereditor?(project = nil) ⇒ Boolean
65 66 67 |
# File 'lib/etna/user.rb', line 65 def is_supereditor? project=nil has_any_role?(:administration, :admin, :editor) end |
#is_superuser?(project = nil) ⇒ Boolean
61 62 63 |
# File 'lib/etna/user.rb', line 61 def is_superuser? project=nil has_any_role?(:administration, :admin) end |
#is_superviewer?(project = nil) ⇒ Boolean
69 70 71 |
# File 'lib/etna/user.rb', line 69 def is_superviewer? project=nil has_any_role?(:administration, :admin, :editor, :viewer) end |
#permissions ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/etna/user.rb', line 23 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 |
#task? ⇒ Boolean
19 20 21 |
# File 'lib/etna/user.rb', line 19 def task? !!@task end |