Module: UcbRailsUser::UsersControllerConcerns

Extended by:
ActiveSupport::Concern
Includes:
Pagy::Backend
Included in:
UsersController
Defined in:
app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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

def create
  uid = params.fetch(:ldap_uid)
  user = nil
  if user = UcbRailsUser.user_class.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



63
64
65
66
67
68
69
70
71
# File 'app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb', line 63

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

  redirect_to(admin_users_path)
end

#editObject



31
32
# File 'app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb', line 31

def edit
end

#impersonate_searchObject



26
27
28
29
# File 'app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb', line 26

def impersonate_search
  result = UcbRailsUser::UserSearch.find_users_by_name(params[:q])
  render json: result.map { |u| { name: u.full_name, id: u.id, uid: u.ldap_uid } }
end

#indexObject



11
12
13
# File 'app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb', line 11

def index
  @pagy, @users = pagy(UcbRailsUser.user_class.all, limit: params[:count])
end

#ldap_searchObject



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

def ldap_search
  # FIXME: this was retrofitted to support typeahead ajax json queries
  if(query = params[:query])
    @lps_entries = UcbRailsUser::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 = UcbRailsUser::LdapPerson::Finder.find_by_first_last(
      params.fetch(:first_name),
      params.fetch(:last_name),
      :sort => :last_first_downcase
    )
    uid_strings = @lps_entries.map { |entry| entry.uid&.to_s }.compact
    @lps_existing_uids = UcbRailsUser.user_class.where(ldap_uid: uid_strings).pluck(:uid)
    render 'ucb_rails_user/lps/search'
  end

end

#newObject



34
35
# File 'app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb', line 34

def new
end

#omni_typeahead_searchObject



102
103
104
105
# File 'app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb', line 102

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

#searchObject



15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb', line 15

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



107
108
109
110
# File 'app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb', line 107

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

#typeahead_searchObject



97
98
99
100
# File 'app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb', line 97

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

#updateObject



55
56
57
58
59
60
61
# File 'app/controllers/concerns/ucb_rails_user/users_controller_concerns.rb', line 55

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