Class: Etna::User

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(params, token = nil) ⇒ User

Returns a new instance of User.

Raises:

  • (ArgumentError)


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

#emailObject (readonly)

Returns the value of attribute email.



17
18
19
# File 'lib/etna/user.rb', line 17

def email
  @email
end

#firstObject (readonly)

Returns the value of attribute first.



17
18
19
# File 'lib/etna/user.rb', line 17

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



17
18
19
# File 'lib/etna/user.rb', line 17

def last
  @last
end

#tokenObject (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

Returns:

  • (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.

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/etna/user.rb', line 76

def can_see_restricted? project
  perm = permissions[project.to_s]
  perm && perm[:restricted]
end

#can_view?(project) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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 = permissions[project.to_s]

  return false unless perm

  return roles.map(&:to_sym).include?(perm[:role])
end

#is_admin?(project) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


61
62
63
# File 'lib/etna/user.rb', line 61

def is_superuser? project=nil
  has_roles(:administration, :admin)
end

#nameObject



39
40
41
# File 'lib/etna/user.rb', line 39

def name
  "#{first} #{last}"
end

#permissionsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/etna/user.rb', line 19

def permissions
  @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

#projectsObject



43
44
45
# File 'lib/etna/user.rb', line 43

def projects
  permissions.keys
end