summaryrefslogtreecommitdiff
path: root/app/controllers/users_controller.rb
blob: 7af8e681faddc3cf8fbe588908db4ca60ef306ec (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class UsersController < ApplicationController
  load_resource :tenant
  load_resource :user_group
  load_and_authorize_resource :user, :through => [:tenant, :user_group]
  
  before_filter :set_and_authorize_parent
  before_filter :spread_breadcrumbs
  
  def index
  end

  def show
    @phone_books = PhoneBook.accessible_by( Ability.new( @user ), :read )
  end

  def new
    @user = @parent.users.build(params[:user])
    @user.male = true
    @user.send_voicemail_as_email_attachment = true
  end

  def create
    @user = @parent.users.build(params[:user])
    if @user.save
      if @parent.class == Tenant
        @parent.tenant_memberships.create(:user => @user)
        if @parent.user_groups.exists?(:name => 'Users')
          @parent.user_groups.where(:name => 'Users').first.user_group_memberships.create(:user => @user)
        end
        redirect_to tenant_user_url( @parent, @user), :notice => t('users.controller.successfuly_created', :resource => @user)
      else
        redirect_to tenant_user_path(@user.current_tenant, @user), :notice => t('users.controller.successfuly_created_and_login', :resource => @user)
      end
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @user.update_attributes(params[:user])
      # Make sure that the flash notice gets rendered in the correct language.
      I18n.locale = @user.language.code.downcase 

      redirect_to tenant_user_path(@user.current_tenant, @user), :notice  => t('users.controller.successfuly_updated')
    else
      render :edit
    end
  end
    
  def destroy
    @user.destroy
    redirect_to @parent, :notice => t('users.controller.successfuly_destroyed')
  end

  def destroy_avatar
    user = User.find(params[:user_id])
    user.remove_image = true  # https://github.com/jnicklas/carrierwave/issues/360
    user.remove_image!
    user.save
    user.reload   
    user.image.remove!
    user.save
    redirect_to @parent, :notice => t('users.controller.avatar_destroyed')
  end

  private
  def set_and_authorize_parent
    @parent = @tenant || @user_group
    authorize! :read, @parent
  end

  def spread_breadcrumbs
    if @tenant
      add_breadcrumb t("users.index.page_title"), tenant_users_path(@tenant)

      if @user && !@user.new_record?
        add_breadcrumb @user, tenant_user_path(@tenant, @user)
      end
    end
  end

end