Class: UsersController

Inherits:
Devise::RegistrationsController
  • Object
show all
Defined in:
app/controllers/users_controller.rb

Overview

@File Name :users_controller.rb

@Company Name :Mindfire Solutions Pvt. Ltd.

@Creator Name :Indranil Mukherjee

@Date Created :2012-06-04

@Date Modified                        :2012-06-14

@Last Modification Details            :Making it as mcms project standard

@Purpose                              :This controller is responsible for managing users

Instance Method Summary collapse

Instance Method Details

#createObject

@Params : No parameter

@Returns                              : Nothing is returned POST mcms/users/
@Purpose                              : Creating a new user


86
87
88
89
90
91
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
# File 'app/controllers/users_controller.rb', line 86

def create

  build_resource # This helper is defined in devise which is building the resource (here user) so that we can have resource to be accessed here


  @all_roles = Role.all # fetch all roles

  if resource.save # saving the resource (here user)

    if resource.active_for_authentication? # (we are not bother about active which is done after confirmation.In our case user is active upon registartion.No confirmation is needed. )

      # saving the roles

      @all_roles.each do |role|

        tmp = role.id.to_s + VALUE



        RolesUser.create!(:user_id => resource.id , :role_id => Role.find_by_title(role.title).id) if params[tmp] == "1"

      end

      flash[:notice] = t(:user_create_success,:user_email => resource.email ,:default => "#{resource.email} successfully created")

      redirect_to mcms_users_path
    else

      @all_roles.each do |role|

        tmp = role.id.to_s + VALUE

        RolesUser.create!(:user_id => resource.id , :role_id => Role.find_by_title(role.title).id) if params[tmp] == "1"

      end

      set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?

      expire_session_data_after_sign_in!

      respond_with resource, :location => (resource)

    end

  else

    clean_up_passwords resource

    respond_with resource

  end

end

#destroy_userObject

@Params : No parameter

@Returns                              : Nothing is returned DELETE mcms/users/1
@Purpose                              : Destroying an existing user


206
207
208
209
210
211
212
213
214
215
216
# File 'app/controllers/users_controller.rb', line 206

def destroy_user

  @user = User.find(params[:id]) # Find the user to be destroyed

  @user.destroy # destroy it

  flash[:notice] = t(:user_destroy_success,:user_email => @user.email,:default => "#{@user.email} successfully deleted")

  redirect_to mcms_users_path #go to all users list

end

#editObject

@Params : No parameter

@Returns                              : Nothing is returned POST mcms/users/1/edit
@Purpose                              : Editing an existing user


144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/users_controller.rb', line 144

def edit

  @all_roles = Role.all

  @user = User.find(params[:id]) # finding the user to be edited

  respond_to do |f|
    f.html   #HTML response
    f.json {render :json => @user} # JSON response
  end

end

#first_userObject



218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/controllers/users_controller.rb', line 218

def first_user
  @user = User.new #creating an instance of user
    unless User.all.empty?
      redirect_to "/users/sign_in"
    else
      respond_to do |format|
        format.html
      end
    end
  
  
end

#first_user_createObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/controllers/users_controller.rb', line 231

def first_user_create
  
  build_resource # This helper is defined in devise which is building the resource (here user) so that we can have resource to be accessed here

  if resource.save # saving the resource (here user)



    # saving the roles

    RolesUser.create!(:user_id => resource.id , :role_id => Role.find_by_title("superuser").id)


    flash[:notice] = t(:user_create_success,:user_email => resource.email ,:default => "#{resource.email} successfully created")

    redirect_to mcms_users_path
  else
    clean_up_passwords resource
    flash[:notice] =  resource.errors.full_messages
    redirect_to "/mcms/first_user"
  end
end

#indexObject

@Params : No parameter

@Returns                              : Nothing is returned GET mcms/users
@Purpose                              : Showing all existing users


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/users_controller.rb', line 52

def index

  @users = User.all # Selecting all users

  respond_to do |f|

    f.html # HTML response

    f.json {render :json => @users} # JSON response

  end

end

#newObject

@Params : No parameter

@Returns                              : Nothing is returned GET mcms/users/new
@Purpose                              : Instantiating a new user


71
72
73
74
75
76
77
78
79
# File 'app/controllers/users_controller.rb', line 71

def new

  @all_roles = Role.all # Roles needs to be there to select

  @user = User.new #creating an instance of user
  
  super # else is upto devise

end

#updateObject

@Params : No parameter

@Returns                              : Nothing is returned POST mcms/users/1/edit
@Purpose                              : Updating an existing user


161
162
163
164
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
# File 'app/controllers/users_controller.rb', line 161

def update

  @user = User.find(params[:id]) # Finding the user to be updated

  @all_roles = Role.all

  # Following we are checking whether password field is kept blank ;so the old password stays.

  params[:user].delete(:password) if params[:user][:password].blank?

  params[:user].delete(:password_confirmation) if params[:user][:password].blank? and params[:user][:password_confirmation].blank?

  # Updating the attributes of users

  if @user.update_attributes(params[:user])

    # Assigning updated roles

    @all_roles.each do |role|

      tmp = role.id.to_s + VALUE

      RolesUser.create!(:user_id => resource.id , :role_id => Role.find_by_title(role.title).id) if params[tmp] == "1" && RolesUser.find_by_role_id_and_user_id(Role.find_by_title(role.title).id,resource.id).nil?

    end

    flash[:notice] = t(:user_update_success,:user_email => @user.email,:default => "#{@user.email} successfully updated")

    redirect_to mcms_users_path

  else

    render :action => 'edit'

  end


end