Class: UsersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/users_controller.rb

Overview

User management within the sharebox site

Instance Method Summary collapse

Methods inherited from ApplicationController

#check_lang, #prepare_attached_docs_request

Instance Method Details

#destroyObject

Delete a specific user
only for admins



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/users_controller.rb', line 30

def destroy
  unless current_user.is_admin?
      flash[:notice] = t('sb.only_admin_may_delete_user')
  else
      if current_user.id.to_i == params[:id].to_i
          # devise can do it but we do not integrate this feature
          flash[:notice] = t('sb.yu_cannot_delete_yur_own_account')
      else
          @user = User.find(params[:id])
          sharedto=@user.shared_folders_by_others
          puts("***************************l'utilisateur a #{sharedto.length} répertoire(s) partagé(s)")
          report=""
          if @user.destroy
            sharedto.each do |stf|
              stf.lists=stf.calc_meta
              puts("****************new meta for folder #{stf.name} are #{stf.lists}")
              unless stf.save
                report="#{report} #{t('sb.folder_metas')} #{stf.name} #{t('sb.shared_to_the_deleted_user')}\n"
                report="#{report} #{t('sb.not_updated')}\n"
                report="#{report} #{t('sb.please_ask_admin_to_update_manually')}\n"
              else
                report="#{report} #{t('sb.folder_metas')} #{stf.name} #{t('sb.shared_to_the_deleted_user')}\n"
                report="#{report} #{t('sb.updated')}\n"
              end
            end
            flash[:notice]="#{t('sb.user')} #{params[:id]} #{t('sb.deleted')}...#{report}"
          else 
            flash[:notice]="#{t('sb.user')} #{params[:id]} #{t('sb.not_deleted')}"
          end
      end
  end
  redirect_to users_path
end

#filter(params) ⇒ Object (private)

return a list of users according to some request parameters a ligth filtering function for users management def filter(groups=nil,statut=nil,melfrag=nil,order=nil)



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/controllers/users_controller.rb', line 208

def filter(params)
  tab=wherestring(params)
  order=params[:order]
  unless order
    order="ID ASC"
  end        
  if tab[0].length>0
    users=User.where(tab).order(order)
  else
    users=User.all.order(order)
  end
  if params[:admin]
    users=fixpractises(users)
  end
  users
end

#fixpractises(users) ⇒ Object (private)

define in the users records, extra fields related to sharing and receiving



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/controllers/users_controller.rb', line 165

def fixpractises(users)
  #SHARING USERS
  sql = <<-SQL
    SELECT distinct users.id 
    from users 
    INNER JOIN shared_folders 
    on users.id = shared_folders.user_id;
  SQL
  sharing_users=[]
  User.find_by_sql(sql).each do |u|
    sharing_users.push(u.id)
  end
  puts("sharing users: #{sharing_users}")
  
  #USERS BEING GRANTED SHARES
  sql = <<-SQL
    SELECT distinct users.id 
    from users 
    INNER JOIN shared_folders 
    on users.id = shared_folders.share_user_id;
  SQL
  users_with_shares=[]
  User.find_by_sql(sql).each do |u|
    users_with_shares.push(u.id)
  end
  puts("users being granted shares: #{users_with_shares}")

  #LOOP on USERS ACTIVE RECORDS
  users.each do |u|
    if sharing_users.include?(u.id)
      u.is_sharing=t('sb.is_sharing')
    end
    if users_with_shares.include?(u.id)  
      u.has_shares=t('sb.has_shares')
    end
  end
  users
end

#get_groupsObject

given a word as param, return a list with the closed groups in the database



110
111
112
113
114
115
116
117
118
# File 'app/controllers/users_controller.rb', line 110

def get_groups
  results={}
  unless params[:groupsfrag]
    results["message"]=t('sb.no_input')
  else
    results=User.where("groups LIKE ?","%#{params[:groupsfrag]}%").distinct.pluck(:groups)
  end
  render json: results
end

#indexObject

Search a list of users according to some filtering parameters and render to json
params are melfrag, statut, groups
if param admin is present, calculates also the sharing practises which are not recorded in the database<b> possible to add a param order (not finalized) please note in a controller, params always exists - its minimal size is 2 with 2 keys : controller and action
if params has got only two keys, render the users management dashboard



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/users_controller.rb', line 71

def index
  color_code="33"
  puts ("\e[#{color_code}m**********we are in the controller #{params[:controller]}\e[0m")
  puts ("\e[#{color_code}m**********params has got #{params.keys.length} key(s)\e[0m")
  puts ("\e[#{color_code}m**********which are : #{params.keys}\e[0m")
  
  #if melfrag=params[:melfrag]
  if params.keys.length>2
    #allusers = User.where("email LIKE ?", "%#{melfrag}%")
    #results=[]
    #allusers.each do |u|
    #  results<< {"email": u.email,"id": u.id}
    #end
    #render json: results
    #users=filter(params[:groups],params[:statut],params[:melfrag],params[:order])
    users=filter(params)
    if params[:admin]
      render json: users.as_json(methods: ["is_sharing","has_shares"])
    else
      render json: users
    end
  else
    unless current_user.is_admin?
      flash[:notice] = t('sb.no_permission')
      redirect_to root_url
    end
  end
end

#showObject

user’s preference dashboard
permits to set the locale and the groups



103
104
105
# File 'app/controllers/users_controller.rb', line 103

def show
  @user=current_user
end

#updateObject

update current user preferences : lang and groups



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/users_controller.rb', line 10

def update
  unless current_user.id == params[:id].to_i
    flash[:notice] = t('sb.no_permission')
    message="___________________________________________#{params[:id]} vs #{current_user.id}"
    puts("\e[31m#{message}\e[0m")
    redirect_to root_url
  else
    if current_user.update(params.require(:user).permit(:lang,:groups))
      I18n.locale=params[:user][:lang]
      flash[:notice]=t('sb.updated')
    else
      flash[:notice]=t('sb.not_updated')
    end
  end
  redirect_to user_path(params[:id])
end

#wherestring(params) ⇒ Object (private)

generate where part of the request



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/users_controller.rb', line 126

def wherestring(params)
  groups=params[:groups]
  statut=params[:statut]
  melfrag=params[:melfrag]
  tab=[]
  request=[]
  tab[0]=""
  if groups
    unless groups=="!"
      request.push("groups like ?")
      tab.push("%#{groups}%")
    else
      request.push("(groups is null or groups = '')")
    end
  end
  if statut
    request.push("statut like ?")
    tab.push("%#{statut}%")
  end
  if melfrag
    puts("************************we have ")
    if Rails.configuration.sharebox["downcase_email_search_autocomplete"]
      melfrag=melfrag.downcase
    end
    unless melfrag.include?("!")
      request.push("email like ?")
      tab.push("%#{melfrag}%")
    else
      request.push("email not like ?")
      tab.push("%#{melfrag}%".gsub("!",""))
    end  
  end
  tab[0]=request.join(" and ")
  puts(tab)
  tab
end