Class: User

Inherits:
ApplicationRecord show all
Defined in:
app/models/user.rb

Overview

The user model

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#has_sharesObject

Returns the value of attribute has_shares.



28
29
30
# File 'app/models/user.rb', line 28

def has_shares
  @has_shares
end

#is_sharingObject

Returns the value of attribute is_sharing.



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

def is_sharing
  @is_sharing
end

Class Method Details

.search(term) ⇒ Object

Return all users with email containing the specific term



238
239
240
# File 'app/models/user.rb', line 238

def self.search(term)
  where('LOWER(email) LIKE :term', term: "%#{term.downcase}%")
end

Instance Method Details

#belongs_to_team?Boolean

check if user belongs to the team if any

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'app/models/user.rb', line 38

def belongs_to_team?
  a = true
  if ENV['TEAM']
    a = self.email.split("@")[1]==ENV.fetch('TEAM')
  end
  a
end

#complete_suidObject

This method is used after a user creation
It checks if the shared_folders table has to be completed (column share_user_id)
Sends an email to the admin



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/user.rb', line 62

def complete_suid
  @shared_folders=SharedFolder.where("share_user_id IS NULL")
  missing_share_user_id_filled=""
  unregistered_emails=""
  @shared_folders.each do |u|
    folder=Folder.find_by_id(u.folder_id)
    share="share (#{u.id}) on folder #{folder.name} (#{u.folder_id})"
    if u.missing_share_user_id?
      if u.fetch_user_id_associated_to_email
        u.share_user_id=u.fetch_user_id_associated_to_email
        if u.save
          missing_share_user_id_filled="#{missing_share_user_id_filled} -> filled share_user_id (#{u.share_user_id}) for #{share}<br><br>"
        end
      else
        unregistered_emails="#{unregistered_emails} -> #{u.share_email} not yet registered for #{share}<br><br>"
      end
    end
  end
  mel_to_admin="#{missing_share_user_id_filled}#{unregistered_emails}"
  if mel_to_admin != ""
    mel_to_admin="<h2>Report - complete_suid user method</h2><br><br>#{mel_to_admin}"
    InformAdminJob.perform_now(self,mel_to_admin)
  end
end

#get_all_emailsObject

Return an hash table (key/value) => (id/email) of all users in a single SQL request



164
165
166
167
168
169
170
# File 'app/models/user.rb', line 164

def get_all_emails
  h = Hash.new
  User.all.each do |u|
    h[u.id] = u.email
  end
  return h
end

#has_asset_ownership?(asset) ⇒ Boolean

Return true if the user owns the asset

Returns:

  • (Boolean)


183
184
185
186
# File 'app/models/user.rb', line 183

def has_asset_ownership?(asset)
  #return true if self.assets.include?(asset)
  return true if asset.user_id==self.id
end

#has_completed_satisfaction?(folder, satisfactions = nil) ⇒ Boolean

return the corresponding satisfaction record if user has answered to a satisfaction survey on the folder return false if user has not answered if a satisfaction list record is given, do not burn a SQL request

Returns:

  • (Boolean)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/models/user.rb', line 216

def has_completed_satisfaction?(folder, satisfactions=nil)
  unless satisfactions
    #return true if Satisfaction.where(folder_id: folder.id, user_id: self.id).length != 0
    result=self.satisfactions.find_by_folder_id(folder.id)
    unless result
      result=false
    end
  else
    result=false
    satisfactions.each do |s|
      if s.folder_id==folder.id && s.user_id==self.id 
        result = s
      end
    end
    
  end
  puts("user model - test if current_user has completed satisfaction - result is #{result}")
  return result
end

#has_ownership?(folder) ⇒ Boolean

Return true if the user owns the folder

Returns:

  • (Boolean)


174
175
176
177
178
179
# File 'app/models/user.rb', line 174

def has_ownership?(folder)
  # not sure but include? does not burn a SQL request
  #return true if self.folders.include?(folder)
  puts("{{{{{{{{{{folder stamped to belong to user #{folder.user_id} and current user is number #{self.id}")
  return true if folder.user_id==self.id
end

#has_shared_access?(folder) ⇒ Boolean

Return true if user has “shared access” on the folder
“shared access” means being owner or being granted of a share
if folder is a subfolder of a folder shared to the user, we consider the user has shared access on the subfolder QUITE SQL COSTLY - do not use heavily - DO NOT USE IN LOOPS !!!!!!!!!!!!!

Returns:

  • (Boolean)


92
93
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/user.rb', line 92

def has_shared_access?(folder)
  puts("shared_access_testing...")
  
  return true if self.is_admin?
  
  # is the folder owned by the user ?
  folders=self.folders
  return true if folders.include?(folder)
  oids=[]
  folders.each do |o|
    oids.push(o.id)
  end
  puts("all folder ids owned by the user: #{oids}")
  
  # has the folder been shared to the user ?
  sharedfolders=self.shared_folders_by_others
  return true if sharedfolders.include?(folder)
  sids=[]
  sharedfolders.each do |s|
    sids.push(s.id)
  end
  puts("all folder ids shared to the user: #{sids}")
  
  # if the user owns or was shared one of the folder's ancestors, he has shared access on the folder
  puts("&&&&&&&&&&&&&&&&getting all ancestors active records")
  ancestors=folder.ancestors
  puts("BEGIN ---------------- ancestors exploration")
  ancestors.each do |ancestor_folder|
    #these two instructions are too costly
    #return true if sharedfolders.include?(ancestor_folder)
    #return true if folders.include?(ancestor_folder)
    return true if sids.include?(ancestor_folder.id)
    return true if oids.include?(ancestor_folder.id)
  end
  puts("END ---------------- ancestors exploration")
  
  # CAUTION - normally the following happens rarely, mostly with the old PRIMITIVE style of browsing, ie breadcrumb navigation
  # it is not really used by the new API-based Ajax browsing style, except with the go_up button !!
  # if you have swarmed a folder, you need to access to the whole tree of the primo ancestor of the folder 
  # by primo ancestor, we mean the root folder which hosts directly or indirectly the folder 
  # we explore all the subfolders directly or indirectly related to the primo ancestor of the folder
  # if one folder belongs to the user, the user is considered to have a shared access
  # HEAVY COST HEAVY COST OMG
  puts("BEGIN ---------------- base searching")
  if folder.is_root?
    base = folder
  else 
    base = ancestors.reverse[0]
  end
  puts("END ---------------- base searching")
  puts("BEGIN ---------------- tree exploration from main root folder")
  base.get_all_sub_folders.each do |sub|
    return true if self.folders.include?(sub)
  end
  
  return false
  
end

#has_shared_folders_from_others?Boolean

Return true if user has been awarded some shared folders by private or admin users

Returns:

  • (Boolean)


190
191
192
# File 'app/models/user.rb', line 190

def has_shared_folders_from_others?
  return self.shared_folders_by_others.length>0
end

#has_su_access?(folder) ⇒ Boolean

super user access on a folder is true if folder is owned or swarmed to the user
a low cost method using the metadatas

Returns:

  • (Boolean)


154
155
156
157
158
159
# File 'app/models/user.rb', line 154

def has_su_access?(folder)
  puts("superuser_access_testing...")
  return true if self.folders.include?(folder)
  return true if folder.get_meta["swarmed_to"]==self.id
  return false
end

#is_admin?Boolean

Return true is user is admin

Returns:

  • (Boolean)


196
197
198
# File 'app/models/user.rb', line 196

def is_admin?
  return true if self.statut == "admin"
end

#is_private?Boolean

Return true if user is private

Returns:

  • (Boolean)


202
203
204
# File 'app/models/user.rb', line 202

def is_private?
  return true if self.statut == "private"
end

#is_public?Boolean

Return true if user is public

Returns:

  • (Boolean)


208
209
210
# File 'app/models/user.rb', line 208

def is_public?
  return true if self.statut == "public"
end

#set_adminObject

This method is used after a user creation
It realize a concrete action only once in theory
It grants, after initial login, admin rights to the first user opening an account
you can use db/seeds.rb as an alternative : rake db:seed



51
52
53
54
55
56
# File 'app/models/user.rb', line 51

def set_admin
  if User.count == 1
    self.statut = "admin"
    self.save
  end
end