resources_controller.rb 1.93 KB
Newer Older
o@immerda.ch's avatar
o@immerda.ch committed
1
2
class ResourcesController < ApplicationController
  def show
o@immerda.ch's avatar
o@immerda.ch committed
3
4
5
    if list_of_canonical_resources.include? params[:kind]
      @resource_name = params[:kind]
    end
o@immerda.ch's avatar
o@immerda.ch committed
6
    begin
o@immerda.ch's avatar
o@immerda.ch committed
7
8
9
10
11
      res = api.my_resources['data']
      if @resource_name
        res = res.select{|uid| uid =~ /^#{@resource_name}:/} || []
      end
      @resources = res
o@immerda.ch's avatar
o@immerda.ch committed
12
13
14
15
16
    rescue ApiBackend::ApiError
      flash[:danger] = :failed
      redirect_to root_path
    end
  end
17
18
19

  def create
    flash[:warning] = nil
o@immerda.ch's avatar
o@immerda.ch committed
20
    @resource_names = list_of_canonical_resources
o@immerda.ch's avatar
o@immerda.ch committed
21
    @resource_quota = api.resource_quota
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    @resource_name = params[:resource_name]
    @resource_name = nil unless @resource_names.include?(@resource_name)
    if @resource_name
      @resource_schema = api.resource_user_schema(@resource_name)['schema']
    end
    if @resource_name == 'schleuder'
      begin
        WkdSrvClient.get(current_user)
      rescue
        @disabled = true
        flash[:warning] = 'missing_gpg_key'
      end
    end
  end

  def do_create
    @resource_name = params[:resource_name]
o@immerda.ch's avatar
o@immerda.ch committed
39
    if list_of_canonical_resources.include? @resource_name
40
      data = {}
Birger Schacht's avatar
Birger Schacht committed
41
42
      data['name'] = params[:localpart]
      data['domain'] = params[:domain]
43
44
      @res = api.resource_self_create(@resource_name, data)

o@immerda.ch's avatar
o@immerda.ch committed
45
      update_account_properties
46
47
48
49
50
      if @resource_name == 'mailman' || @resource_name == 'schleuder'
        flash[:success] = :resource_created
      else
        flash[:success] = :resource_created_now
      end
51
    end
o@immerda.ch's avatar
o@immerda.ch committed
52
    redirect_to action: :show, kind: @resource_name
53
54
55
56
  rescue ApiError => e
    flash[:danger] = e.api_msg
    redirect_to new_resource_path
  end
o@immerda.ch's avatar
o@immerda.ch committed
57
58
59
60
61
62
63
64
65
66

  def update
    uid = params[:uid]
    pw = params[:password]

    if params[:delete].present?
      @res = api.resource_delete(uid, pw)
      flash[:success] = :success
    end

o@immerda.ch's avatar
o@immerda.ch committed
67
    redirect_to action: :show, kind: params[:kind]
o@immerda.ch's avatar
o@immerda.ch committed
68
69
  rescue ApiError => e
    flash[:danger] = e.api_msg
o@immerda.ch's avatar
o@immerda.ch committed
70
    redirect_to action: :show, kind: params[:kind]
o@immerda.ch's avatar
o@immerda.ch committed
71
72
  end

o@immerda.ch's avatar
o@immerda.ch committed
73
end