Class: Refinery::Admin::UsersController

Inherits:
Refinery::AdminController show all
Defined in:
authentication/app/controllers/refinery/admin/users_controller.rb

Instance Method Summary (collapse)

Methods included from BaseController

#admin?, #searching?

Methods included from Refinery::ApplicationController

#admin?, #error_404, #from_dialog?, #home_page?, #just_installed?, #local_request?, #login?

Instance Method Details

- (Object) create



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'authentication/app/controllers/refinery/admin/users_controller.rb', line 17

def create
  @user = Refinery::User.new(params[:user].except(:roles))
  @selected_plugin_names = params[:user][:plugins] || []
  @selected_role_names = params[:user][:roles] || []

  if @user.save
    @user.plugins = @selected_plugin_names
    # if the user is a superuser and can assign roles according to this site's
    # settings then the roles are set with the POST data.
    unless current_refinery_user.has_role?(:superuser) and Refinery::Authentication.superuser_can_assign_roles
      @user.add_role(:refinery)
    else
      @user.roles = @selected_role_names.collect { |r| Refinery::Role[r.downcase.to_sym] }
    end

    redirect_to refinery.admin_users_path,
                :notice => t('created', :what => @user.username, :scope => 'refinery.crudify')
  else
    render :action => 'new'
  end
end

- (Object) edit



39
40
41
42
43
# File 'authentication/app/controllers/refinery/admin/users_controller.rb', line 39

def edit
  redirect_unless_user_editable!

  @selected_plugin_names = @user.plugins.collect(&:name)
end

- (Object) new



12
13
14
15
# File 'authentication/app/controllers/refinery/admin/users_controller.rb', line 12

def new
  @user = Refinery::User.new
  @selected_plugin_names = []
end

- (Object) update



45
46
47
48
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
75
76
77
78
79
80
81
82
# File 'authentication/app/controllers/refinery/admin/users_controller.rb', line 45

def update
  redirect_unless_user_editable!

  # Store what the user selected.
  @selected_role_names = params[:user].delete(:roles) || []
  unless current_refinery_user.has_role?(:superuser) and Refinery::Authentication.superuser_can_assign_roles
    @selected_role_names = @user.roles.pluck(:title)
  end
  @selected_plugin_names = params[:user][:plugins]

  # Prevent the current user from locking themselves out of the User manager or backend
  if current_refinery_user.id == @user.id && # If editing self
     ((@selected_plugin_names.present? && # If we're submitting plugins
       @selected_plugin_names.exclude?("refinery_users")) || # And we're removing user plugin access
       @selected_role_names.map(&:downcase).exclude?("refinery")) # Or we're removing the refinery role

    flash.now[:error] = t('cannot_remove_user_plugin_from_current_user', :scope => 'refinery.admin.users.update')
    render :edit
  else
    # Store the current plugins and roles for this user.
    @previously_selected_plugin_names = @user.plugins.collect(&:name)
    @previously_selected_roles = @user.roles
    @user.roles = @selected_role_names.collect { |r| Refinery::Role[r.downcase.to_sym] }
    if params[:user][:password].blank? and params[:user][:password_confirmation].blank?
      params[:user].except!(:password, :password_confirmation)
    end

    if @user.update_attributes(params[:user])
      redirect_to refinery.admin_users_path,
                  :notice => t('updated', :what => @user.username, :scope => 'refinery.crudify')
    else
      @user.plugins = @previously_selected_plugin_names
      @user.roles = @previously_selected_roles
      @user.save
      render :edit
    end
  end
end