Class: User
- Inherits:
-
JSONModel
- Object
- JSONModel
- User
- Defined in:
- backend/app/model/user.rb,
frontend/app/models/user.rb
Defined Under Namespace
Classes: MailError, PermissionNotFound
Constant Summary collapse
- @@unlisted_user_ids =
nil
Class Method Summary collapse
-
.ADMIN_USERNAME ⇒ Object
-
.become_user(context, username) ⇒ Object
-
.broadcast_changes ⇒ Object
-
.create_from_json(json, opts = {}) ⇒ Object
-
.establish_session(context, backend_session, username) ⇒ Object
-
.login(username, password) ⇒ Object
-
.make_admin_if_requested(obj, json) ⇒ Object
-
.PUBLIC_USERNAME ⇒ Object
-
.recover_password(email) ⇒ Object
-
.refresh_permissions(context) ⇒ Object
-
.SEARCH_USERNAME ⇒ Object
-
.sequel_to_jsonmodel(objs, opts = {}) ⇒ Object
-
.STAFF_USERNAME ⇒ Object
-
.store_permissions(permissions, context) ⇒ Object
-
.token_login(username, token) ⇒ Object
-
.unlisted_user_ids ⇒ Object
Instance Method Summary collapse
-
#add_to_groups(groups, delete_all_for_repo_id = false) ⇒ Object
-
#anonymous? ⇒ Boolean
-
#before_save ⇒ Object
-
#can?(permission_code, opts = {}) ⇒ Boolean
True if a user has access to perform ‘permission’ in ‘repo_id’.
-
#delete ⇒ Object
-
#derived_permissions ⇒ Object
-
#permissions ⇒ Object
-
#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object
-
#validate ⇒ Object
Class Method Details
.ADMIN_USERNAME ⇒ Object
93 94 95 |
# File 'backend/app/model/user.rb', line 93 def self.ADMIN_USERNAME "admin" end |
.become_user(context, username) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'frontend/app/models/user.rb', line 48 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_changes ⇒ Object
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
10 11 12 13 14 15 16 17 |
# File 'frontend/app/models/user.rb', line 10 def self.establish_session(context, backend_session, username) context.session[:session] = backend_session["session"] (backend_session["user"]["permissions"], context) context.session[:user_uri] = backend_session["user"]["uri"] context.session[:user] = username end |
.login(username, password) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'frontend/app/models/user.rb', line 35 def self.login(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_USERNAME ⇒ Object
103 104 105 |
# File 'backend/app/model/user.rb', line 103 def self.PUBLIC_USERNAME AppConfig[:public_username] end |
.recover_password(email) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'frontend/app/models/user.rb', line 66 def self.recover_password(email) uri = JSONModel(:user).uri_for("reset-password") begin response = JSONModel::HTTP.post_form(uri, email: email) = ASUtils.json_parse(response.body) if response.code == '200' return {status: :success} else case ['error'] when "UserMailer::MailError" return {status: :error, message: I18n.t("user._frontend.messages.password_reset_fail")} when "NotFoundException" return {status: :not_found, message: I18n.t("user._frontend.messages.error_not_found", email: email)} else return {status: :error, message: ['error']} end end rescue Exception => e Rails.logger.error(e) return { status: :error, error: I18n.t("user._frontend.messages.password_reset_fail") } end end |
.refresh_permissions(context) ⇒ Object
20 21 22 23 24 25 26 |
# File 'frontend/app/models/user.rb', line 20 def self.(context) user = self.find('current-user') if user (user., context) end end |
.SEARCH_USERNAME ⇒ Object
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_USERNAME ⇒ Object
108 109 110 |
# File 'backend/app/model/user.rb', line 108 def self.STAFF_USERNAME AppConfig[:staff_username] end |
.store_permissions(permissions, context) ⇒ Object
29 30 31 32 |
# File 'frontend/app/models/user.rb', line 29 def self.(, context) context.send(:cookies).signed[:archivesspace_permissions] = 'ZLIB:' + Zlib::Deflate.deflate(ASUtils.to_json(Permissions.pack())) context.session[:last_permission_refresh] = Time.now.to_i end |
.token_login(username, token) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'frontend/app/models/user.rb', line 90 def self.token_login(username, token) uri = JSONModel(:user).uri_for("#{username}/#{token}") response = JSONModel::HTTP.post_form(uri) if response.code == '200' ASUtils.json_parse(response.body) else nil end end |
.unlisted_user_ids ⇒ Object
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
217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'backend/app/model/user.rb', line 217 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
130 131 132 |
# File 'backend/app/model/user.rb', line 130 def anonymous? false end |
#before_save ⇒ Object
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’
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?(, opts = {}) if .include?(.to_s) return true end = Permission[: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 .nil? if [: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 => .id, :repo_id => [self.class.active_repository, global_repo.id].reject(&:nil?)). count) >= 1) end |
#delete ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'backend/app/model/user.rb', line 231 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_permissions ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'backend/app/model/user.rb', line 135 def = 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]} .map {|p| Permission.(p) }.flatten.uniq end |
#permissions ⇒ Object
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 212 213 214 |
# File 'backend/app/model/user.rb', line 179 def result = {} derived = # 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) = [] 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 << row[:permission_code] end end # assume all users can edit themselves for now << 'edit_user_self' # Attach permissions in the global repository under the symbolic name too result[Repository.GLOBAL] = + 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 |
#validate ⇒ Object
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 |