Class: Group

Inherits:
Sequel::Model
  • Object
show all
Defined in:
backend/app/model/group.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ADMIN_GROUP_CODEObject



13
14
15
# File 'backend/app/model/group.rb', line 13

def self.ADMIN_GROUP_CODE
  'administrators'
end

.broadcast_changesObject



141
142
143
# File 'backend/app/model/group.rb', line 141

def self.broadcast_changes
  Notifications.notify("REFRESH_ACLS")
end

.create_from_json(json, opts = {}) ⇒ Object



86
87
88
89
90
91
92
93
# File 'backend/app/model/group.rb', line 86

def self.create_from_json(json, opts = {})
  obj = super
  set_members(obj, json)
  set_permissions(obj, json)

  broadcast_changes
  obj
end

.PUBLIC_GROUP_CODEObject



23
24
25
# File 'backend/app/model/group.rb', line 23

def self.PUBLIC_GROUP_CODE
  'publicanonymous'
end

.SEARCHINDEX_GROUP_CODEObject



18
19
20
# File 'backend/app/model/group.rb', line 18

def self.SEARCHINDEX_GROUP_CODE
  'searchindex'
end

.sequel_to_jsonmodel(objs, opts = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'backend/app/model/group.rb', line 117

def self.sequel_to_jsonmodel(objs, opts = {})
  jsons = super

  jsons.zip(objs).each do |json, obj|
    if opts[:with_members]
      json.member_usernames = obj.user.map {|user| user[:username]}
    end

    json.grants_permissions = obj.permission.map {|permission| permission[:permission_code]}
  end

  jsons
end

.set_members(obj, json) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'backend/app/model/group.rb', line 39

def self.set_members(obj, json)
  nonusers = []
  (json.member_usernames or []).map {|username|
    user = User[:username => username]
    if not user
      nonusers << username
    end
  }

  if nonusers.length > 0
    if nonusers.length == 1
      raise UserNotFoundException.new("User #{nonusers[0]} does not exist")
    else
      raise UserNotFoundException.new("Users #{nonusers.join(', ')} do not exist")
    end
  end

  obj.remove_all_user
  (json.member_usernames or []).map {|username|
    user = User[:username => username]
    obj.add_user(user) if user
  }
end

.set_permissions(obj, json) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'backend/app/model/group.rb', line 64

def self.set_permissions(obj, json)
  obj.remove_all_permission
  (json.grants_permissions or []).each do |permission_code|

    next if Permission.derived?(permission_code)

    permission = Permission[:permission_code => permission_code]

    if permission.nil?
      raise "Unknown permission code: #{permission_code}"
    end

    if permission[:level] == 'global'
      Log.error("You can't assign a global permission to a repository")
      raise AccessDeniedException.new("You can't assign a global permission to a repository")
    end

    obj.add_permission(permission)
  end
end

.STAFF_GROUP_CODEObject



27
28
29
# File 'backend/app/model/group.rb', line 27

def self.STAFF_GROUP_CODE
  'staffsystem'
end

.transfer_all(source_repository, target_repository) ⇒ Object



155
156
157
# File 'backend/app/model/group.rb', line 155

def self.transfer_all(source_repository, target_repository)
  # Groups aren't candidates for being transferred, so we don't call super.
end

Instance Method Details

#before_saveObject



32
33
34
35
36
# File 'backend/app/model/group.rb', line 32

def before_save
  super

  self.group_code_norm = self.group_code.downcase
end

#deleteObject



146
147
148
149
150
151
152
# File 'backend/app/model/group.rb', line 146

def delete
  remove_all_permission
  remove_all_user

  super
  self.class.broadcast_changes
end

#grant(permission_code) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'backend/app/model/group.rb', line 106

def grant(permission_code)
  permission = Permission[:permission_code => permission_code.to_s]

  if self.class.db[:group_permission].filter(:group_id => self.id,
                                             :permission_id => permission.id).empty?

    add_permission(permission)
  end
end

#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object



96
97
98
99
100
101
102
103
# File 'backend/app/model/group.rb', line 96

def update_from_json(json, opts = {}, apply_nested_records = true)
  super
  self.class.set_members(self, json) if opts[:with_members]
  self.class.set_permissions(self, json)

  self.class.broadcast_changes
  self.id
end

#validateObject



132
133
134
135
136
137
138
# File 'backend/app/model/group.rb', line 132

def validate
  super
  self.group_code_norm = self.group_code.downcase
  validates_unique([:repo_id, :group_code_norm],
                   :message => "Group code must be unique within a repository")
  map_validation_to_json_property([:repo_id, :group_code_norm], :group_code)
end