Module: UcbRailsUser::Concerns::UsersController

Extended by:
ActiveSupport::Concern
Included in:
UsersController
Defined in:
app/controllers/ucb_rails_user/concerns/users_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 35

def create
  uid = params.fetch(:ldap_uid)
  user = nil
  if user = User.find_by_ldap_uid(uid)
    flash[:warning] = "User already exists"
  else
    begin
      user = UcbRailsUser::UserLdapService.create_user_from_uid(uid)
      flash[:notice] = "Record created"
    rescue Exception => e
      raise e
      flash[:danger] = "Unable to create new user - please try again"
      return redirect_to new_admin_user_path()
    end
  end
  redirect_to edit_admin_user_path(user)
end

#destroyObject



61
62
63
64
65
66
67
68
69
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 61

def destroy
  if @user.destroy
    flash[:notice] = 'Record deleted'
  else
    flash[:error] = @user.errors[:base].first
  end

  redirect_to(admin_users_path)
end

#editObject



29
30
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 29

def edit
end

#indexObject



10
11
12
13
14
15
16
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 10

def index
  @users = User.all
  respond_to do |format|
    format.html { @users = User.all }
    format.json { render json: UcbRails::UsersDatatable.new(view_context).as_json }
  end
end

#ldap_searchObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 71

def ldap_search
  # FIXME: this was retrofitted to support typeahead ajax json queries
  if(query = params[:query])
    @lps_entries = UcbRails::OmniUserTypeahead.new.ldap_results(query)
    @lps_entries.map!{|entry|
      attrs = entry.attributes.tap{|attrs| attrs["first_last_name"] = "#{attrs['first_name']} #{attrs['last_name']}" }
      attrs.as_json
    }

    render json: @lps_entries

  else
    @lps_entries = UcbRails::LdapPerson::Finder.find_by_first_last(
      params.fetch(:first_name),
      params.fetch(:last_name),
      :sort => :last_first_downcase
    )
    @lps_existing_uids = User.where(ldap_uid: @lps_entries.map(&:uid)).pluck(:uid)
    render 'lps/search'
  end

end

#newObject



32
33
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 32

def new
end

#omni_typeahead_searchObject



99
100
101
102
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 99

def omni_typeahead_search
  uta = UcbRails::OmniUserTypeahead.new
  render json: uta.results(params.fetch(:query))
end

#searchObject



18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 18

def search
  @results = UcbRailsUser::LdapPerson::Finder.find_by_attributes(
    {
      givenname: params.fetch(:first_name),
      sn: params.fetch(:last_name),
      employeenumber: params.fetch(:employee_id)
    },
    sort: :last_first_downcase
  )
end

#toggle_superuserObject



104
105
106
107
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 104

def toggle_superuser
  current_user.try(:superuser!, !superuser?)
  redirect_to root_path
end

#typeahead_searchObject



94
95
96
97
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 94

def typeahead_search
  uta = UcbRails::UserTypeahead.new
  render json: uta.results(params.fetch(:query))
end

#updateObject



53
54
55
56
57
58
59
# File 'app/controllers/ucb_rails_user/concerns/users_controller.rb', line 53

def update
  if @user.update_attributes(user_params)
    redirect_to(admin_users_path, notice: 'Record updated')
  else
    render("edit")
  end
end