Class: Group

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

Constant Summary

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ASModel

all_models, included, update_publish_flag, update_suppressed_flag

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Class Method Details

.ADMIN_GROUP_CODEObject



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

def self.ADMIN_GROUP_CODE
  'administrators'
end

.broadcast_changesObject



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

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

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



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

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

  broadcast_changes
  obj
end

.PUBLIC_GROUP_CODEObject



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

def self.PUBLIC_GROUP_CODE
  'publicanonymous'
end

.SEARCHINDEX_GROUP_CODEObject



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

def self.SEARCHINDEX_GROUP_CODE
  'searchindex'
end

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



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

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



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

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



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

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



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

def self.STAFF_GROUP_CODE
  'staffsystem'
end

.transfer_all(source_repository, target_repository) ⇒ Object



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

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



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

def before_save
  super

  self.group_code_norm = self.group_code.downcase
end

#deleteObject



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

def delete
  remove_all_permission
  remove_all_user

  super
  self.class.broadcast_changes
end

#grant(permission_code) ⇒ Object



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

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



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

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



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

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