Class: RolesController

Inherits:
ApplicationController show all
Includes:
Models
Defined in:
app/controllers/roles_controller.rb

Overview

@File Name :roles_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 creating,
                                       editing,destroying roles

Instance Method Summary collapse

Methods included from Models

#get_all_plugins, #get_models, get_module_name, #get_relations, #manipulated_array

Instance Method Details

#createObject

@Params : Hash

@Returns                              : Nothing is returned POST mcms/roles/
@Purpose                              : Instantiating a new role


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
150
151
152
153
154
155
156
157
158
159
160
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/controllers/roles_controller.rb', line 95

def create
  # instantiating a role with passed parameters
  @role = Role.new(params[:role])

  # getting all plugins(modules) in current application
  
  @all_plugins = get_all_plugins

  respond_to do |format|

    if @role.save # saving the role

      # iterating existing plugins and creating plugin access for the role being saved
      # we have used conditional statements to set boolean values for crud access to each module.

      @all_plugins.each do |plugin|

        p = Plugin.new

        p.role_module = plugin.last

        models = ExistingModel.find_by_plugin_name(plugin.last)

        if models.nil?
          plugin.each do |pl|

            existing_model = ExistingModel.new
            existing_model.plugin_name = plugin.last
            existing_model.model_name = pl
            existing_model.save

          end

        end

        all = plugin.last + ALL

        read = plugin.last + READ

        create = plugin.last + CREATE

        update = plugin.last + UPDATE

        destroy = plugin.last + DESTROY

        if params[all] == "1"

          p.role_manage  = true # setting access control

        else

          p.role_manage = false # resetting access control

        end


        if params[read] == "1" || params[all] == "1"

          p.role_read = true

        else

          p.role_read = false

        end

        if params[create] == "1" || params[all] == "1"

          p.role_create = true

        else

          p.role_create = false

        end

        if params[update] == "1" || params[all] == "1"

          p.role_update = true

        else

          p.role_update = false

        end

        if params[destroy] == "1" || params[all] == "1"

          p.role_destroy = true

        else

          p.role_destroy = false

        end

        p.role_id = @role.id # associating the role being saved

        p.save # saving each plugin access control

      end

      format.html  { redirect_to(roles_path,
          :notice => t(:role_create_success,:default => 'Role was successfully created.')) } # HTML response

      format.json  { render :json => @role,
        :status => :created, :location => @role } # json response
    else

      # if any exception occured recreate the role
      format.html  { render :action => "new" } # HTML response

      format.json  { render :json => @role.errors,
        :status => :unprocessable_entity } # json response

    end

  end

end

#destroyObject

@Params : Integer

@Returns                              : Nothing is returned DELETE mcms/roles/1/
@Purpose                              : Destroying an existing role


365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'app/controllers/roles_controller.rb', line 365

def destroy

  @role = Role.find(params[:id]) # Select a role to be destroyed

  @role.destroy # delete the role

  respond_to do |format|

    format.html { redirect_to roles_url } # HTML response

    format.json { head :no_content }  # JSON response

  end

end

#editObject

@Params : Integer

@Returns                              : Nothing is returned PUT mcms/roles/1/edit
@Purpose                              : Updating an existing role


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'app/controllers/roles_controller.rb', line 221

def edit

  @all_plugins = get_all_plugins # fetching all the plugins

  @role = Role.find(params[:id]) # fetching the role to be updated

  respond_to do |f|

    f.html      #HTML response

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

  end

end

#indexObject

@Params : No parameter

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


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

def index

  @roles = Role.all # fetching all roles

  respond_to do |f|

    f.html  # output as HTML

    f.json {render :json => @roles} # output as json

  end

end

#newObject

@Params : No parameter

@Returns                              : Nothing is returned GET mcms/roles/new
@Purpose                              : Instantiating a new role


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/roles_controller.rb', line 73

def new

  @all_plugins = get_all_plugins # getting all plugins(modules) in current application

  @role = Role.new # instantiating a new role

  respond_to do |f|

    f.html # output as HTML

    f.json {render :json => @role} # output as json

  end

end

#updateObject

@Params : Integer

@Returns                              : Nothing is returned PUT mcms/roles/1/edit
@Purpose                              : Updating an existing role


242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'app/controllers/roles_controller.rb', line 242

def update

  # Finding the role to be updated by ID
  @role = Role.find(params[:id])

  # Fetching all the available plugins
  @all_plugins = get_all_plugins

  respond_to do |format|

    if @role.update_attributes(params[:role]) # updating the role

      # plugins access control is also updated with conditional checks.

      @all_plugins.each do |plugin|

        p = Plugin.find_by_role_module(plugin.last)

        if p.nil?

          p = Plugin.new

        end
        models = ExistingModel.find_by_plugin_name(plugin.last)

        if models.nil?

          plugin.each do |pl|

            existing_model = ExistingModel.new
            existing_model.plugin_name = plugin.last
            existing_model.model_name = pl
            existing_model.save

          end

        end

        p.role_module = plugin.last

        all = plugin.last + ALL

        read = plugin.last + READ

        create = plugin.last + CREATE

        update = plugin.last + UPDATE

        destroy = plugin.last + DESTROY

        if params[all] == "1"

          p.role_manage  = true
        else
          p.role_manage = false
        end


        if params[read] == "1" || params[all] == "1"

          p.role_read = true
        else
          p.role_read = false
        end

        if params[create] == "1" || params[all] == "1"

          p.role_create = true
        else
          p.role_create = false
        end

        if params[update] == "1" || params[all] == "1"

          p.role_update = true

        else

          p.role_update = false

        end

        if params[destroy] == "1" || params[all] == "1"

          p.role_destroy = true

        else

          p.role_destroy = false

        end

        p.role_id = @role.id

        p.save

      end


      format.html  { redirect_to(roles_path,
          :notice => t(:role_update_success,:default => 'Role was successfully updated.')) } # HTML response

      format.json  { head :no_content } # JSON Response

    else

      format.html  { render :action => "edit" } # Failed HTML response to update again

      format.json  { render :json => @role.errors,
        :status => :unprocessable_entity } # Failed JSON response to update again

    end

  end

end