Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#synced_with_gramObject

synced_with_gram : True if last sync with gram data succeed (false if not). false if object never tried to sync at runtime. Auto sync performed at connection via CAS



14
15
16
# File 'app/models/user.rb', line 14

def synced_with_gram
  @synced_with_gram
end

Class Method Details

.find_by_id_or_hruid_or_uuid(id) ⇒ Object



132
133
134
# File 'app/models/user.rb', line 132

def self.find_by_id_or_hruid_or_uuid(id)
  where("CAST(id AS CHAR)=:id OR uuid=:id OR hruid=:id",id: id).take
end

.omniauth(auth_data, signed_in_resource = nil) ⇒ Object

Return an User to login after Omniauth authentification. This users is retrieve in database with hruid or created on the fly from CAS data



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/user.rb', line 94

def self.omniauth(auth_data, signed_in_resource=nil)

  logger.debug "=================================="
  logger.debug "Connexion depuis le CAS uid : "+auth_data[:uid]
  logger.debug "Infos de connection :"
  logger.debug auth_data.inspect

  return nil unless auth_data[:extra][:uuid]

  # auth_data : take a look on Users::OmniauthCallbacksController
  unless user = User.find_by_uuid(auth_data[:extra][:uuid])
    user = User.new(
        email: auth_data[:info][:email],
        password: Devise.friendly_token[0,20],
        hruid: auth_data[:uid],
        firstname: auth_data[:extra][:firstname],
        lastname: auth_data[:extra][:lastname],
        uuid: auth_data[:extra][:uuid],
    )
    user.save
  end

  if user.persisted?
    user.update_from_gram
    user
  else
    logger.error "Donnees renvoyes par le CAS invalide : "+auth_data.to_s+" ---- "+user.errors.messages.inspect
    nil
  end
end

.search(query) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/user.rb', line 141

def self.search(query)
  sql_query= nil
  if query
    sql_query=<<END_SQL
LOWER(users.firstname) LIKE :like_query
OR LOWER(users.lastname) LIKE :like_query
OR LOWER(CONCAT(users.firstname,' ',users.lastname)) LIKE :like_query
OR LOWER(users.hruid) LIKE :like_query
OR LOWER(users.uuid) = :query
OR LOWER(email_redirect_accounts.redirect) = :query
END_SQL
  end

  self.where(sql_query,query: query.to_s.downcase,like_query: "%#{query.to_s.downcase}%")
end

Instance Method Details

#add_role(role_name) ⇒ Object

Add or change role



37
38
39
# File 'app/models/user.rb', line 37

def  add_role (role_name)
  self.update_attribute(:role_id,Role.find_or_create_by(name: role_name).id)
end

#fullnameObject

Return users firstname and lastname



127
128
129
# File 'app/models/user.rb', line 127

def fullname
  firstname + " " + lastname
end

#has_role?(role_name) ⇒ Boolean

TEMPLATE FUNCTIONS ###############################################

Check current role

Returns:

  • (Boolean)


30
31
32
# File 'app/models/user.rb', line 30

def has_role? (role_name)
  self.role ? self.role.name==(role_name.to_s) : false
end

#is_gadz?Boolean

Returns:

  • (Boolean)


136
137
138
139
# File 'app/models/user.rb', line 136

def is_gadz?
  self.update_attribute( :is_gadz, GramV2Client::Account.find(self.uuid).is_gadz)
  self.is_gadz
end

#next_sync_allowed_atObject



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

def next_sync_allowed_at
  self.last_gram_sync_at ? self.last_gram_sync_at + 5.minutes : Time.now
end

#remove_role(role_name = nil) ⇒ Object

Delete current role if any



43
44
45
# File 'app/models/user.rb', line 43

def  remove_role (role_name=nil)
  self.update_attribute(:role_id,nil) unless !self.role ||(role_name && role_name.to_s != self.role.name)
end

#syncable?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/user.rb', line 83

def syncable?
  uuid.present?
end

#update_from_gramObject

Update local users data with data contained in GrAM



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/user.rb', line 49

def update_from_gram
  self.synced_with_gram = false
  if self.syncable?
    begin
      gram_data= GramV2Client::Account.find(self.uuid)
      self.email=gram_data.email
      self.firstname=gram_data.firstname
      self.lastname=gram_data.lastname
      self.last_gram_sync_at = Time.now
      self.hruid = gram_data.hruid
      self.is_gadz = gram_data.is_gadz
      self.avatar_url = gram_data.avatar_url
      if self.save
        self.synced_with_gram = true
        return self
      else
        return false
      end
    rescue ActiveResource::ResourceNotFound
      logger.error "[GrAM] Utilisateur introuvable : hruid = #{self.hruid} uuid = #{self.uuid}"
      return false
    rescue ActiveResource::ServerError
      logger.error "[GrAM] Connexion au serveur impossible"
      return false
    rescue ActiveResource::UnauthorizedAccess, ActiveResource::ForbiddenAccess
      logger.error "[GrAM] Connexion au serveur impossible : verifier identifiants"
      return false
    end
  else
    return false
  end

end