Class: SimpleUser::AdminUsersController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#set_user_return_to

Instance Method Details

#authorize_admin_user_managerObject



126
127
128
129
130
131
# File 'app/controllers/simple_user/admin_users_controller.rb', line 126

def authorize_admin_user_manager
  if !can? :manage, AdminUser
    flash[:error] = "Access denied"
    redirect_to root_url
  end
end

#createObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/simple_user/admin_users_controller.rb', line 49

def create
  @admin_user = AdminUser.new(params[:admin_user])
  @required_password = true

  roles = []
  if can? :manage, Role
    roles = params[:admin_user][:temporal_roles].split(",")
  end
  params[:admin_user].delete(:temporal_roles)

  respond_to do |format|
    if @admin_user.save
      if can? :manage, Role
        roles.map{ |role|
          @admin_user.add_role role.strip
        }
      end

      format.html { redirect_to @admin_user, notice: 'Admin was successfully created.' }
      format.json { render json: @admin_user, status: :created, location: @admin_user }
    else
      format.html { render action: "new" }
      format.json { render json: @admin_user.errors, status: :unprocessable_entity }
    end
  end
end

#destroyObject



116
117
118
119
120
121
122
123
124
# File 'app/controllers/simple_user/admin_users_controller.rb', line 116

def destroy
  @admin_user = AdminUser.find(params[:id])
  @admin_user.destroy

  respond_to do |format|
    format.html { redirect_to admin_users_url }
    format.json { head :no_content }
  end
end

#editObject



37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/simple_user/admin_users_controller.rb', line 37

def edit
  @admin_user = AdminUser.find(params[:id])

  if @admin_user.has_role? :admin
    flash[:error] = "Access denied"
    redirect_to root_url
  end

  @admin_user.get_roles
  @required_password = false
end

#indexObject



9
10
11
12
13
14
15
16
# File 'app/controllers/simple_user/admin_users_controller.rb', line 9

def index
  @admin_users = AdminUser.get_editable_admins_except(current_admin_user.id)

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @admin_users }
  end
end

#newObject



27
28
29
30
31
32
33
34
35
# File 'app/controllers/simple_user/admin_users_controller.rb', line 27

def new
  @admin_user = AdminUser.new
  @required_password = true

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @admin_user }
  end
end

#showObject



18
19
20
21
22
23
24
25
# File 'app/controllers/simple_user/admin_users_controller.rb', line 18

def show
  @admin_user = AdminUser.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @admin_user }
  end
end

#updateObject



76
77
78
79
80
81
82
83
84
85
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
# File 'app/controllers/simple_user/admin_users_controller.rb', line 76

def update
  @admin_user = AdminUser.find(params[:id])
  @required_password = false

  if @admin_user.has_role? :admin
    flash[:error] = "Access denied"
    redirect_to root_url
  end

  roles = []
  if can? :manage, Role
    roles = params[:admin_user][:temporal_roles].split(",")
  end
  params[:admin_user].delete(:temporal_roles)

  if params[:admin_user][:password].blank?
    params[:admin_user].delete(:password)
    params[:admin_user].delete(:password_confirmation)
  end

  respond_to do |format|
    if @admin_user.update_attributes(params[:admin_user])
      if can? :manage, Role
        @admin_user.roles.destroy_all

        roles.map{ |role|
          @admin_user.add_role role.strip
        }
      end

      format.html { redirect_to @admin_user, notice: 'Admin was successfully updated.' }
      format.json { head :no_content }
    else
      @admin_user.get_roles
      format.html { render action: "edit" }
      format.json { render json: @admin_user.errors, status: :unprocessable_entity }
    end
  end
end