Class: Caboose::PermissionsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_add, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_delete, #admin_edit, #admin_index, #admin_json, #admin_json_single, #admin_update, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#before_actionObject


5
6
7
# File 'app/controllers/caboose/permissions_controller.rb', line 5

def before_action
  @page = Page.page_with_uri(request.host_with_port, '/admin')
end

#createObject


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/caboose/permissions_controller.rb', line 37

def create
  return if !user_is_allowed('permissions', 'add')
  
  resp = StdClass.new({
      'error' => nil,
      'redirect' => nil
  })
  
  perm = Permission.new()
  perm.resource = params[:resource]
  perm.action   = params[:action2]
  
  if (perm.resource.strip.length == 0)
    resp.error = "The resource is required."
  elsif (perm.action.strip.length == 0)
    resp.error = "The action is required."
  else      
    perm.save
    resp.redirect = "/admin/permissions/#{perm.id}/edit"
  end
  render json: resp
end

#destroyObject


82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/caboose/permissions_controller.rb', line 82

def destroy
  return if !user_is_allowed('permissions', 'delete')
  perm = Permission.find(params[:id])
  perm.destroy
  
  resp = StdClass.new({
    'redirect' => '/admin/permissions'
  })
  render json: resp
end

#editObject


31
32
33
34
# File 'app/controllers/caboose/permissions_controller.rb', line 31

def edit
  return if !user_is_allowed('permissions', 'edit')
  @permission = Permission.find(params[:id])
end

#indexObject


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/caboose/permissions_controller.rb', line 10

def index
  return if !user_is_allowed('permissions', 'view')
  
  @gen = PageBarGenerator.new(params, {
		  'resource'  => nil
		},{
		  'model'       => 'Caboose::Permission',
	    'sort'			  => 'resource, action',
		  'desc'			  => false,
		  'base_url'		=> '/admin/permissions'
	})
	@permissions = @gen.items    	
end

#newObject


25
26
27
28
# File 'app/controllers/caboose/permissions_controller.rb', line 25

def new
  return if !user_is_allowed('permissions', 'add')
  @permission = Permission.new
end

#optionsObject


95
96
97
98
99
100
# File 'app/controllers/caboose/permissions_controller.rb', line 95

def options
  return if !user_is_allowed('permissions', 'view')
  perms = Permission.reorder('resource, action').all
  options = perms.collect { |p| { 'value' => p.id, 'text' => "#{p.resource}_#{p.action}"}}
  render json: options
end

#updateObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/caboose/permissions_controller.rb', line 61

def update
  return if !user_is_allowed('permissions', 'edit')

  resp = StdClass.new     
  perm = Permission.find(params[:id])

  save = true
  params.each do |name,value|
    case name
	  	when "resource"
	  	  perm.resource = value
	  	when "action2"
	  	  perm.action = value
	  end
	end
	
	resp.success = save && perm.save
	render json: resp
end