Class: Admin::ComponentsController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/components_controller.rb

Overview

Handling components

Instance Method Summary collapse

Instance Method Details

#add_administratorObject

put /admin/components/:slug/administrators/:user_id



80
81
82
83
84
85
86
# File 'app/controllers/admin/components_controller.rb', line 80

def add_administrator
  if @handler.administrator?
    @handler.component.add_administrator(User.find_by(id: params[:user_id]))
  end

  head :no_content
end

#add_privilegeObject

put /admin/components/:slug/users/:user_id/privileges/:privilege_slug



98
99
100
101
102
103
104
105
# File 'app/controllers/admin/components_controller.rb', line 98

def add_privilege
  if @handler.administrator?
    user = User.find_by(id: params[:user_id])
    @handler.component.add_privilege(user, params[:privilege_slug])
  end

  head :no_content
end

#delete_parameterObject

delete /admin/components/:slug/parameters/:parameter_slug



49
50
51
52
53
54
55
56
# File 'app/controllers/admin/components_controller.rb', line 49

def delete_parameter
  if @handler.allow('settings')
    @handler.component.parameters.delete(params[:parameter_slug])
    @handler.component.save
  end

  head :no_content
end

#indexObject

get /admin/components



8
9
10
# File 'app/controllers/admin/components_controller.rb', line 8

def index
  @collection = BiovisionComponent.list_for_administration
end

#privilegesObject

get /admin/components/:slug/privileges



59
60
61
62
# File 'app/controllers/admin/components_controller.rb', line 59

def privileges
  error = 'Viewing privileges is not allowed'
  handle_http_401(error) unless @handler.administrator?
end

#remove_administratorObject

put /admin/components/:slug/administrators/:user_id



89
90
91
92
93
94
95
# File 'app/controllers/admin/components_controller.rb', line 89

def remove_administrator
  if @handler.administrator?
    @handler.component.remove_administrator(User.find_by(id: params[:user_id]))
  end

  head :no_content
end

#remove_privilegeObject

put /admin/components/:slug/users/:user_id/privileges/:privilege_slug



108
109
110
111
112
113
114
115
# File 'app/controllers/admin/components_controller.rb', line 108

def remove_privilege
  if @handler.administrator?
    user = User.find_by(id: params[:user_id])
    @handler.component.remove_privilege(user, params[:privilege_slug])
  end

  head :no_content
end

#settingsObject

get /admin/components/:slug/settings



19
20
21
22
# File 'app/controllers/admin/components_controller.rb', line 19

def settings
  error = 'Viewing settings is not allowed'
  handle_http_401(error) unless @handler.allow?('settings')
end

#showObject

get /admin/components/:slug



13
14
15
16
# File 'app/controllers/admin/components_controller.rb', line 13

def show
  error = 'Viewing component is not allowed'
  handle_http_401(error) unless @handler.allow?
end

#update_parameterObject

patch /admin/components/:slug/parameters



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

def update_parameter
  if @handler.allow('settings')
    slug = param_from_request(:key, :slug).downcase
    value = param_from_request(:key, :value)

    @handler[slug] = value
  end

  head :no_content
end

#update_privilegesObject

patch /admin/components/:slug/privileges



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/admin/components_controller.rb', line 65

def update_privileges
  if @handler.administrator?
    user = User.find_by(id: params[:user_id])

    if user.nil?
      handle_http_404('Cannot find user') if user.nil?
    else
      @entity = @handler.update_privileges(user)
    end
  else
    handle_http_401('Updating privileges is not allowed')
  end
end

#update_settingsObject

patch /admin/components/:slug/settings



25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/admin/components_controller.rb', line 25

def update_settings
  if @handler.allow('settings')
    new_settings = params.dig(:component, :settings).permit!
    @handler.settings = new_settings.to_h
    flash[:notice] = t('admin.components.update_settings.success')
    redirect_to(admin_component_settings_path(slug: params[:slug]))
  else
    handle_http_401('Changing settings is not allowed')
  end
end