Class: Permissions

Inherits:
Object
  • Object
show all
Defined in:
frontend/app/models/permissions.rb

Class Method Summary collapse

Class Method Details

.pack(repo_permissions_map) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'frontend/app/models/permissions.rb', line 3

def self.pack(repo_permissions_map)
  permission_set = repo_permissions_map.values.flatten.uniq

  result = {}
  result['key'] = permission_set

  result['perms'] = Hash[repo_permissions_map.map do |repo_uri, granted_permissions|

                           packed_permissions = permission_set.map {|permission|
                             granted_permissions.include?(permission) ? "1" : "0"
                           }.join("")

                           [repo_uri, packed_permissions]
                         end]

  result
end

.user_can?(packed_permission_map, repo_uri, permission) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'frontend/app/models/permissions.rb', line 22

def self.user_can?(packed_permission_map, repo_uri, permission)
  position = packed_permission_map['key'].index(permission)

  if !position
    # Any permission not in our key list isn't granted in any repo
    return false
  end

  # Otherwise, the permission is granted if there's a "1" in the appropriate
  # position
  packed_permissions = packed_permission_map['perms'][repo_uri]
  packed_permissions && packed_permissions[position] == "1"
end