Class: Kaui::AdminAllowedUsersController

Inherits:
EngineController
  • Object
show all
Defined in:
app/controllers/kaui/admin_allowed_users_controller.rb

Constant Summary

Constants included from EngineControllerUtil

EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD

Instance Method Summary collapse

Methods inherited from EngineController

#check_for_redirect_to_tenant_screen, #current_ability, #current_user, #options_for_klient, #populate_account_details, #retrieve_allowed_users_for_current_user, #retrieve_tenants_for_current_user

Instance Method Details

#add_tenantObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/kaui/admin_allowed_users_controller.rb', line 87

def add_tenant
  allowed_user = Kaui::AllowedUser.find(params.require(:allowed_user).require(:id))

  if !current_user.root?
    redirect_to admin_allowed_user_path(allowed_user.id), :alert => 'Only the root user can set tenants for user'
    return
  end

  tenants = []
  params.each do |tenant, _|
    tenant_info = tenant.split('_')
    next if tenant_info.size != 2 or tenant_info[0] != 'tenant'
    tenants << tenant_info[1]
  end

  tenants_for_current_user = retrieve_tenants_for_current_user
  tenants = (Kaui::Tenant.where(:id => tenants).select { |tenant| tenants_for_current_user.include?(tenant.kb_tenant_id) }).map(&:id)

  allowed_user.kaui_tenant_ids = tenants

  redirect_to admin_allowed_user_path(allowed_user.id), :notice => 'Successfully set tenants for user'
end

#createObject



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
# File 'app/controllers/kaui/admin_allowed_users_controller.rb', line 16

def create
  @is_killbill_managed = nil
  @allowed_user = Kaui::AllowedUser.new(allowed_user_params)

  existing_user = Kaui::AllowedUser.find_by_kb_username(@allowed_user.kb_username)
  unless existing_user.blank?
    flash[:error] = "User with name #{@allowed_user.kb_username} already exists!"
    @roles = roles_for_user(existing_user)
    render :new and return
  else
    if params[:external] == '1'
      # Create locally only
      @allowed_user.save!
    else
      @allowed_user.create_in_kb!(params.require(:password) ,
                                params[:roles].blank? ? [] : params[:roles].split(','),
                                current_user.kb_username,
                                params[:reason],
                                params[:comment],
                                options_for_klient)
    end

    redirect_to kaui_engine.admin_allowed_user_path(@allowed_user.id), :notice => 'User was successfully configured'
  end
end

#destroyObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/kaui/admin_allowed_users_controller.rb', line 74

def destroy
  allowed_user = Kaui::AllowedUser.find(params.require(:id))

  if allowed_user
    # Delete locally and in KB
    allowed_user.destroy_in_kb!(current_user.kb_username, params[:reason], params[:comment], options_for_klient)
    redirect_to kaui_engine.admin_allowed_users_path, :notice => 'User was successfully deleted'
  else
    flash[:error] = "User #{params.require(:id)} not found"
    redirect_to kaui_engine.admin_allowed_users_path
  end
end

#editObject



52
53
54
55
56
57
# File 'app/controllers/kaui/admin_allowed_users_controller.rb', line 52

def edit
  @allowed_user = Kaui::AllowedUser.find(params.require(:id))
  @is_killbill_managed = killbill_managed?(@allowed_user, options_for_klient)

  @roles = roles_for_user(@allowed_user)
end

#indexObject



5
6
7
# File 'app/controllers/kaui/admin_allowed_users_controller.rb', line 5

def index
  @allowed_users = retrieve_allowed_users_for_current_user
end

#newObject



9
10
11
12
13
14
# File 'app/controllers/kaui/admin_allowed_users_controller.rb', line 9

def new
  @allowed_user = Kaui::AllowedUser.new
  @is_killbill_managed = true

  @roles = []
end

#showObject

Raises:

  • (ActiveRecord::RecordNotFound)


42
43
44
45
46
47
48
49
50
# File 'app/controllers/kaui/admin_allowed_users_controller.rb', line 42

def show
  @allowed_user = Kaui::AllowedUser.find(params.require(:id))
  raise ActiveRecord::RecordNotFound.new("Could not find user #{@allowed_user.id}") unless (current_user.root? || @allowed_user.kb_username == current_user.kb_username)

  @roles = roles_for_user(@allowed_user)

  tenants_for_current_user = retrieve_tenants_for_current_user
  @tenants = Kaui::Tenant.all.select { |tenant| tenants_for_current_user.include?(tenant.kb_tenant_id) }
end

#updateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/kaui/admin_allowed_users_controller.rb', line 59

def update
  @allowed_user = Kaui::AllowedUser.find(params.require(:id))

  @allowed_user.description = params[:allowed_user][:description].presence

  @allowed_user.update_in_kb!(params[:password].presence,
                              params[:roles].blank? ? nil : params[:roles].split(','),
                              current_user.kb_username,
                              params[:reason],
                              params[:comment],
                              options_for_klient)

  redirect_to kaui_engine.admin_allowed_user_path(@allowed_user.id), :notice => 'User was successfully updated'
end