Class: User

Inherits:
ApplicationRecord show all
Includes:
TimeZoneConcern, UserAuthenticationConcern, UserMasqueradeConcern, UserSearchConcern
Defined in:
app/models/user.rb

Overview

Represents a user in the application. Users are shared across all instances.

Defined Under Namespace

Classes: Email, EmailsController, Identity, InstancePreloadService, MasqueradesController, OmniauthCallbacksController, ProfilesController, RegistrationsController, SessionsController

Constant Summary collapse

SYSTEM_USER_ID =
0
DELETED_USER_ID =
-1

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TimeZoneConcern

#time_zone

Methods included from UserMasqueradeConcern

#can_masquerade?

Class Method Details

.deletedUser

Finds the Deleted user.

Same as the System user, this account cannot be logged into.

Returns:


34
35
36
37
38
# File 'app/models/user.rb', line 34

def deleted
  @deleted ||= find(User::DELETED_USER_ID)
  raise 'No deleted user. Did you run rake db:seed?' unless @deleted
  @deleted
end

.systemUser

Finds the System user.

This account cannot be logged into (because it has no email and a null password), and the User Authentication Concern explicitly rejects any user with the system user ID.

Returns:


23
24
25
26
27
# File 'app/models/user.rb', line 23

def system
  @system ||= find(User::SYSTEM_USER_ID)
  raise 'No system user. Did you run rake db:seed?' unless @system
  @system
end

Instance Method Details

#build_from_invitation(invitation) ⇒ Object

Update the user using the info from invitation.

Parameters:


96
97
98
99
100
101
102
# File 'app/models/user.rb', line 96

def build_from_invitation(invitation)
  self.name = invitation.name
  self.email = invitation.email
  skip_confirmation!
  course_users.build(course: invitation.course, name: invitation.name, role: invitation.role,
                     creator: self, updater: self)
end

#built_in?Boolean

Gets whether the current user is one of the the built in users.

Returns:

  • (Boolean)

78
79
80
# File 'app/models/user.rb', line 78

def built_in?
  id == User::SYSTEM_USER_ID || id == User::DELETED_USER_ID
end

#set_next_email_as_primaryBoolean

Pick the default email and set it as primary email. This method would immediately set the attributes in the database.

Returns:

  • (Boolean)

    True if the new email was set as primary, false if failed or next email cannot be found.


87
88
89
90
91
# File 'app/models/user.rb', line 87

def set_next_email_as_primary
  return false unless default_email_record

  default_email_record.update_attributes(primary: true)
end