summaryrefslogtreecommitdiff
path: root/users/app/controllers/users_controller.rb
blob: 9325bc0d1b08be334255307666014739f5a448a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class UsersController < ApplicationController

  before_filter :authorize, :only => [:show, :edit, :update, :destroy]
  before_filter :fetch_user, :only => [:show, :edit, :update, :destroy]
  before_filter :set_anchor, :only => [:edit, :update]
  before_filter :authorize_admin, :only => [:index]

  respond_to :json, :html

  def index
    if params[:query]
      @users = User.by_login.startkey(params[:query]).endkey(params[:query].succ)
    else
      @users = User.by_created_at.descending
    end
    @users = @users.limit(10)
    respond_with @users.map(&:login).sort
  end

  def new
    @user = User.new
  end

  def create
    @user = User.create(params[:user])
    respond_with @user
  end

  def edit
    @email_alias = LocalEmail.new
  end

  def update
    @user.attributes = params[:user]
    if @user.changed? and @user.save
      flash[:notice] = t(:user_updated_successfully)
    elsif @user.email_aliases.last and !@user.email_aliases.last.valid?
      @email_alias = @user.email_aliases.pop
    end
    respond_with @user, :location => edit_user_path(@user, :anchor => @anchor)
  end

  def destroy
    @user.destroy
    redirect_to admin? ? users_path : login_path
  end

  protected

  def fetch_user
    # authorize filter has been checked first, so won't get here unless authenticated
    @user = User.find_by_param(params[:id])
    if !@user and admin?
      redirect_to users_path, :alert => t(:no_such_thing, :thing => 'user')
      return
    end
    access_denied unless admin? or (@user == current_user)
  end

  def set_anchor
    @anchor = email_settings? ? :email : :account
  end

  def email_settings?
    params[:user] &&
    params[:user].keys.detect{|key| key.index('email')}
  end
end