Class: User

Inherits:
JSONModel
  • Object
show all
Includes:
ASModel
Defined in:
backend/app/model/user.rb,
frontend/app/models/user.rb

Defined Under Namespace

Classes: PermissionNotFound

Constant Summary collapse

@@unlisted_user_ids =
nil

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_USERNAMEObject



93
94
95
# File 'backend/app/model/user.rb', line 93

def self.ADMIN_USERNAME
  "admin"
end

.become_user(context, username) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'frontend/app/models/user.rb', line 45

def self.become_user(context, username)
  return false if username == "admin"
  uri = JSONModel(:user).uri_for("#{username}/become-user")

  response = JSONModel::HTTP.post_form(uri)

  if response.code == '200'
    backend_session = ASUtils.json_parse(response.body)

    self.establish_session(context, backend_session, username)

    true
  else
    false
  end
end

.broadcast_changesObject



88
89
90
# File 'backend/app/model/user.rb', line 88

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

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'backend/app/model/user.rb', line 12

def self.create_from_json(json, opts = {})
  if !opts[:is_hidden_user]
    agent = JSONModel(:agent_person).from_hash(
              {:publish => false,
              :agent_sha1 => SecureRandom.hex,
              :names => [{
                :primary_name => json.name,
                :source => 'local',
                :rules => 'local',
                :name_order => 'direct',
                :sort_name_auto_generate => true
            }]}, raise_errors = true, trusted = true)

    CrudHelpers.with_record_conflict_reporting(AgentPerson, agent) do
      agent_obj = AgentPerson.create_from_json(agent, :system_generated => true, :skip_sha => true)

      opts['agent_record_type'] = :agent_person
      opts['agent_record_id'] = agent_obj.id
    end
  end

  obj = super(json, opts)
  make_admin_if_requested(obj, json)
  obj
end

.establish_session(context, backend_session, username) ⇒ Object



7
8
9
10
11
12
13
14
# File 'frontend/app/models/user.rb', line 7

def self.establish_session(context, backend_session, username)
  context.session[:session] = backend_session["session"]

  store_permissions(backend_session["user"]["permissions"], context)

  context.session[:user_uri] = backend_session["user"]["uri"]
  context.session[:user] = username
end

.login(username, password) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'frontend/app/models/user.rb', line 32

def self.(username, password)
  uri = JSONModel(:user).uri_for("#{username}/login")

  response = JSONModel::HTTP.post_form(uri, :password => password)

  if response.code == '200'
    ASUtils.json_parse(response.body)
  else
    nil
  end
end

.make_admin_if_requested(obj, json) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'backend/app/model/user.rb', line 45

def self.make_admin_if_requested(obj, json)
  return if !RequestContext.get(:apply_admin_access)

  # Nothing to do if these already agree
  begin
    return if (json.is_admin === obj.can?(:administer_system))
  rescue PermissionNotFound
    # System is being bootstrapped and permissions aren't here yet.  That's
    # fine.
  end

  RequestContext.in_global_repo do
    admins_group = Group.this_repo[:group_code => Group.ADMIN_GROUP_CODE]

    if admins_group
      if json.is_admin
        admins_group.add_user(obj)
      else
        admins_group.remove_user(obj)
      end

      self.broadcast_changes
    end
  end
end

.PUBLIC_USERNAMEObject



103
104
105
# File 'backend/app/model/user.rb', line 103

def self.PUBLIC_USERNAME
  AppConfig[:public_username]
end

.refresh_permissions(context) ⇒ Object



17
18
19
20
21
22
23
# File 'frontend/app/models/user.rb', line 17

def self.refresh_permissions(context)
  user = self.find('current-user')

  if user
    store_permissions(user.permissions, context)
  end
end

.SEARCH_USERNAMEObject



98
99
100
# File 'backend/app/model/user.rb', line 98

def self.SEARCH_USERNAME
  AppConfig[:search_username]
end

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'backend/app/model/user.rb', line 72

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

  jsons.zip(objs).each do |json, obj|
    if obj.agent_record_id
      json['agent_record'] = {'ref' => uri_for(obj.agent_record_type, obj.agent_record_id)}
    end

    if obj.can?(:administer_system)
      json['is_admin'] = true
    end
  end
  jsons
end

.STAFF_USERNAMEObject



108
109
110
# File 'backend/app/model/user.rb', line 108

def self.STAFF_USERNAME
  AppConfig[:staff_username]
end

.store_permissions(permissions, context) ⇒ Object



26
27
28
29
# File 'frontend/app/models/user.rb', line 26

def self.store_permissions(permissions, context)
  context.send(:cookies).signed[:archivesspace_permissions] = 'ZLIB:' + Zlib::Deflate.deflate(ASUtils.to_json(Permissions.pack(permissions)))
  context.session[:last_permission_refresh] = Time.now.to_i
end

.unlisted_user_idsObject



113
114
115
# File 'backend/app/model/user.rb', line 113

def self.unlisted_user_ids
  @@unlisted_user_ids ||= User.filter(:is_hidden_user => 1).collect {|user| user.id}
end

Instance Method Details

#add_to_groups(groups, delete_all_for_repo_id = false) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
# File 'backend/app/model/user.rb', line 214

def add_to_groups(groups, delete_all_for_repo_id = false)
  if delete_all_for_repo_id
    groups_ids = self.class.db[:group].where(:repo_id => delete_all_for_repo_id).select(:id)
    self.class.db[:group_user].where(:user_id => self.id, :group_id => groups_ids).delete
  end

  Array(groups).each do |group|
    group.add_user(self)
  end

  self.class.broadcast_changes
end

#anonymous?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'backend/app/model/user.rb', line 130

def anonymous?
  false
end

#before_saveObject



117
118
119
120
121
# File 'backend/app/model/user.rb', line 117

def before_save
  super

  self.username = self.username.downcase
end

#can?(permission_code, opts = {}) ⇒ Boolean

True if a user has access to perform ‘permission’ in ‘repo_id’

Returns:

  • (Boolean)


153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'backend/app/model/user.rb', line 153

def can?(permission_code, opts = {})
  if derived_permissions.include?(permission_code.to_s)
    return true
  end

  permission = Permission[:permission_code => permission_code.to_s]
  global_repo = Repository[:repo_code => Repository.GLOBAL]

  # False if the permission does not exist (or the derived permission is not assigned to this user)
  return false if permission.nil?

  if permission[:level] == "repository" && self.class.active_repository.nil?
    raise("Problem when checking permission: #{permission.permission_code} " +
          "is a repository-level permission, but no repository was set")
  end

  ((self.class.db[:group].
                        join(:group_user, :group_id => :id).
                        join(:group_permission, :group_id => :group_id).
                        filter(:user_id => self.id,
                               :permission_id => permission.id,
                               :repo_id => [self.class.active_repository, global_repo.id].reject(&:nil?)).
                        count) >= 1)
end

#deleteObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'backend/app/model/user.rb', line 228

def delete
  raise AccessDeniedException.new("Can't delete system user") if self.is_system_user == 1

  DBAuth.delete_user(self.username)

  # transfer all import jobs to the admin user
  admin_user = User.select(:id).where( :username => "admin" ).first
  Job.filter(:owner_id => self.id).update( :owner_id => admin_user.id )

  Preference.filter(:user_id => self.id).delete
  self.remove_all_group

  super

  if self.agent_record_id
    AgentPerson[self.agent_record_id].delete
  end
end

#derived_permissionsObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'backend/app/model/user.rb', line 135

def derived_permissions
  actual_permissions =
    self.class.db[:group].
         join(:group_user, :group_id => :id).
         join(:group_permission, :group_id => :group_id).
         join(:permission, :id => :permission_id).
         filter(:user_id => self.id).
         select(:permission_code).map {|row| row[:permission_code]}


  actual_permissions.map {|p| Permission.derived_permissions_for(p) }.flatten.uniq
end

#permissionsObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'backend/app/model/user.rb', line 179

def permissions
  result = {}

  derived = derived_permissions


  # Crikey...
  ds = self.class.db[:group].
    join(:group_user, :group_id => :id).
    join(:group_permission, :group_id => :group_id).
    join(:permission, :id => :permission_id).
    join(:repository, :id => :group__repo_id).
    filter(:user_id => self.id).
    distinct.
    select(Sequel.qualify(:repository, :id).as(:repo_id), Sequel.qualify(:repository, :repo_code).as(:repo_code), :permission_code)

  global_permissions = []

  ds.each do |row|
    repository_uri = JSONModel(:repository).uri_for(row[:repo_id])
    result[repository_uri] ||= derived.clone
    result[repository_uri] << row[:permission_code]

    if row[:repo_code] == Repository.GLOBAL
      global_permissions << row[:permission_code]
    end
  end

  # Attach permissions in the global repository under the symbolic name too
  result[Repository.GLOBAL] = global_permissions + derived

  result
end

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



39
40
41
42
# File 'backend/app/model/user.rb', line 39

def update_from_json(json, opts = {}, apply_nested_records = true)
  self.class.make_admin_if_requested(self, json)
  super
end

#validateObject



124
125
126
127
# File 'backend/app/model/user.rb', line 124

def validate
  validates_unique(:username,
                   :message => "Username '#{self.username}' is already in use")
end