Class: Locomotive::Account

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/locomotive/account.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_api_token(site, email, password) ⇒ String

Create the API token which will be passed to all the requests to the Locomotive API. It requires the credentials of an account with admin role. If an error occurs (invalid account, …etc), this method raises an exception that has to be caught somewhere.

Parameters:

  • site (Site)

    The site where the authentication request is made

  • email (String)

    The email of the account

  • password (String)

    The password of the account

Returns:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/locomotive/account.rb', line 61

def self.create_api_token(site, email, password)
  raise 'The request must contain the user email and password.' if email.blank? or password.blank?

   = self.where(:email => email.downcase).first

  raise 'Invalid email or password.' if .nil?

  .ensure_authentication_token!

  if not .valid_password?(password) # TODO: check admin roles
    raise 'Invalid email or password.'
  end

  .authentication_token
end

.invalidate_api_token(token) ⇒ String

Logout the user responding to the token passed in parameter from the API. An exception is raised if no account corresponds to the token.

Parameters:

  • token (String)

    The API token created by the create_api_token method.

Returns:



84
85
86
87
88
89
90
91
92
# File 'app/models/locomotive/account.rb', line 84

def self.invalidate_api_token(token)
   = self.where(:authentication_token => token).first

  raise 'Invalid token.' if .nil?

  .reset_authentication_token!

  token
end

Instance Method Details

#admin?Boolean

Tell if the account has admin privileges or not. Actually, an account is considered as an admin if it owns at least one admin membership in all its sites.

Returns:



46
47
48
# File 'app/models/locomotive/account.rb', line 46

def admin?
  Site.where(:memberships => { '$elemMatch' => { :account_id => self._id, :role => :admin } }).count > 0
end

#devise_mailerObject



94
95
96
# File 'app/models/locomotive/account.rb', line 94

def devise_mailer
  Locomotive::DeviseMailer
end

#nameObject

validations ##



23
# File 'app/models/locomotive/account.rb', line 23

field :name

#remember_created_atObject

devise fields (need to be declared since 2.x) ##



9
# File 'app/models/locomotive/account.rb', line 9

field :remember_created_at,     :type => Time

#sitesObject

methods ##



36
37
38
# File 'app/models/locomotive/account.rb', line 36

def sites
  @sites ||= Site.where('memberships.account_id' => self._id)
end