ucb_rails 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. data/README.md +34 -4
  2. data/app/controllers/ucb_rails/admin/base_controller.rb +4 -0
  3. data/app/controllers/ucb_rails/admin/users_controller.rb +70 -0
  4. data/app/controllers/ucb_rails/bootstrap_controller.rb +34 -0
  5. data/app/controllers/ucb_rails/controller_methods.rb +67 -6
  6. data/app/controllers/ucb_rails/home_controller.rb +5 -0
  7. data/app/controllers/ucb_rails/ldap_person_search_controller.rb +15 -0
  8. data/app/controllers/ucb_rails/sessions_controller.rb +33 -8
  9. data/app/helpers/ucb_rails/admin/users_helper.rb +14 -0
  10. data/app/helpers/ucb_rails/extractable_helper.rb +17 -0
  11. data/app/helpers/ucb_rails/icons_helper.rb +7 -0
  12. data/app/helpers/ucb_rails/ldap_person_search_helper.rb +21 -0
  13. data/app/helpers/ucb_rails/lps_typeahead_helper.rb +50 -0
  14. data/app/helpers/ucb_rails/renderer/base.rb +52 -0
  15. data/app/helpers/ucb_rails/renderer/ldap_person_search_result_link.rb +81 -0
  16. data/app/models/ucb_rails/ldap_person/entry.rb +54 -0
  17. data/app/models/ucb_rails/ldap_person/finder.rb +67 -0
  18. data/app/models/ucb_rails/ldap_person/test_finder.rb +50 -0
  19. data/app/models/ucb_rails/user.rb +28 -0
  20. data/app/models/ucb_rails/user_ldap_service.rb +50 -0
  21. data/app/models/ucb_rails/user_session_manager/active_in_user_table.rb +37 -0
  22. data/app/models/ucb_rails/user_session_manager/admin_in_user_table.rb +13 -0
  23. data/app/models/ucb_rails/user_session_manager/base.rb +44 -0
  24. data/app/models/ucb_rails/user_session_manager/in_people_ou.rb +27 -0
  25. data/app/models/ucb_rails/user_session_manager/in_people_ou_add_to_users_table.rb +19 -0
  26. data/app/models/ucb_rails/user_session_manager/ldap_person_user_wrapper.rb +29 -0
  27. data/app/models/ucb_rails/user_table_active.rb +3 -0
  28. data/app/views/ucb_rails/admin/users/_form.html.haml +16 -0
  29. data/app/views/ucb_rails/admin/users/_user.html.haml +12 -0
  30. data/app/views/ucb_rails/admin/users/edit.html.haml +3 -0
  31. data/app/views/ucb_rails/admin/users/index.html.haml +40 -0
  32. data/app/views/ucb_rails/lps/_form.html.haml +16 -0
  33. data/app/views/ucb_rails/lps/_modal.html.haml +6 -0
  34. data/app/views/ucb_rails/lps/_results.html.haml +21 -0
  35. data/app/views/ucb_rails/lps/search.js.haml +2 -0
  36. data/config/routes.rb +18 -1
  37. data/lib/generators/ucb_rails/install_generator.rb +27 -23
  38. data/lib/generators/ucb_rails/templates/README +13 -0
  39. data/lib/generators/ucb_rails/templates/app/assets/javascripts/application.js +19 -0
  40. data/lib/generators/ucb_rails/templates/app/assets/javascripts/lps.js +71 -0
  41. data/lib/generators/ucb_rails/templates/app/assets/javascripts/lps_typeahead.js +56 -0
  42. data/lib/generators/ucb_rails/templates/app/assets/javascripts/typeahead.js +8 -0
  43. data/lib/generators/ucb_rails/templates/app/assets/stylesheets/application.css +14 -0
  44. data/lib/generators/ucb_rails/templates/app/assets/stylesheets/bootstrap_and_overrides.css.scss +3 -0
  45. data/lib/generators/ucb_rails/templates/app/assets/stylesheets/ucb_rails.css.scss +51 -0
  46. data/lib/generators/ucb_rails/templates/app/helpers/ucb_rails/application_helper.rb +12 -0
  47. data/lib/generators/ucb_rails/templates/app/views/layouts/_developer.html.haml +13 -0
  48. data/lib/generators/ucb_rails/templates/app/views/layouts/_footer.html.haml +5 -0
  49. data/lib/generators/ucb_rails/templates/app/views/layouts/_navigation.html.haml +25 -0
  50. data/lib/generators/ucb_rails/templates/app/views/layouts/application.html.haml +22 -0
  51. data/lib/generators/ucb_rails/templates/config/initializers/local/active_record/dom_and_haml.rb +61 -0
  52. data/lib/generators/ucb_rails/templates/config/initializers/local/haml_buffer.rb +20 -0
  53. data/lib/generators/ucb_rails/templates/config/initializers/local/simple_form.rb +142 -0
  54. data/lib/generators/ucb_rails/templates/config/initializers/local/simple_form_bootstrap.rb +45 -0
  55. data/lib/generators/ucb_rails/templates/config/initializers/local/ucb_rails.rb +45 -0
  56. data/lib/generators/ucb_rails/templates/config/locales/simple_form.en.yml +26 -0
  57. data/lib/generators/ucb_rails/templates/db/migrate/create_users.rb +23 -0
  58. data/lib/generators/ucb_rails/templates/lib/templates/haml/scaffold/_form.html.haml +10 -0
  59. data/lib/ucb_rails/engine.rb +22 -1
  60. data/lib/ucb_rails/log_tagger.rb +20 -0
  61. data/lib/ucb_rails/version.rb +1 -1
  62. data/lib/ucb_rails.rb +16 -0
  63. metadata +312 -10
  64. data/lib/generators/ucb_rails/templates/initializer.rb +0 -4
data/README.md CHANGED
@@ -1,10 +1,17 @@
1
1
  # UCB Rails
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/ucb_rails.png)](http://badge.fury.io/rb/ucb_rails)
4
+ [![Build Status](https://travis-ci.org/stevedowney/ucb_rails.png)](https://travis-ci.org/stevedowney/ucb_rails)
5
+ [![Coverage Status](https://coveralls.io/repos/stevedowney/ucb_rails/badge.png?branch=master)](https://coveralls.io/r/stevedowney/ucb_rails?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/stevedowney/ucb_rails.png)](https://codeclimate.com/github/stevedowney/ucb_rails)
7
+
3
8
  Get a jump start on your Rails project at UCB. Includes:
4
9
 
5
10
  * CalNet authentication with [omniauth-cas](https://github.com/dlindahl/omniauth-cas)
6
11
  * LDAP integration with [ucb_ldap](https://rubygems.org/gems/ucb_ldap)
7
12
  * includes several other gems including:
13
+ * [bootstrap-view-helpers](https://github.com/stevedowney/bootstrap-view-helpers)
14
+ * [user_announcements](https://github.com/stevedowney/user_announcements)
8
15
  * [rails_environment](https://github.com/stevedowney/rails_environment)
9
16
 
10
17
  ## Installation
@@ -12,14 +19,37 @@ Get a jump start on your Rails project at UCB. Includes:
12
19
  Add it to your Gemfile
13
20
 
14
21
  ```ruby
15
- gem install 'ucb_rails'
22
+ gem 'ucb_rails'
23
+
24
+ # bleeding edge
25
+ gem 'ucb_rails', git: 'https://github.com/stevedowney/ucb_rails'
26
+ ```
27
+
28
+ From the command line, install the `ucb_rails` gem:
29
+
30
+ ```sh
31
+ bundle install
32
+ rails g ucb_rails:install
33
+ ```
34
+
35
+ Run installers for included gems:
36
+
37
+ ```sh
38
+ rails generate user_announcements:install
39
+ rails generate simple_form:install --bootstrap
40
+ ```
41
+
42
+ Run migrations:
43
+
44
+ ```sh
45
+ rake db:migrate
16
46
  ```
17
47
 
18
- From the command line:
48
+ Remove superseded files:
19
49
 
20
50
  ```sh
21
- $ bundle install
22
- $ rails g ucb_rails:install
51
+ rm public/index.html
52
+ rm app/views/layouts/application.html.erb
23
53
  ```
24
54
 
25
55
  Restart your server and point your browser to:
@@ -0,0 +1,4 @@
1
+ class UcbRails::Admin::BaseController < ApplicationController
2
+ before_filter :ensure_admin_user
3
+
4
+ end
@@ -0,0 +1,70 @@
1
+ class UcbRails::Admin::UsersController < UcbRails::Admin::BaseController
2
+ before_filter :find_user, :only => [:edit, :update, :destroy]
3
+
4
+ def index
5
+ respond_to do |format|
6
+ format.html { @users = UcbRails::User.all }
7
+ # format.json { render json: UsersDatatable.new(view_context) }
8
+ end
9
+ end
10
+
11
+ def edit
12
+ end
13
+
14
+ def create
15
+ uid = params.fetch(:uid)
16
+ if user = UcbRails::User.find_by_uid(uid)
17
+ flash[:warning] = "User already exists"
18
+ else
19
+ user = UcbRails::UserLdapService.create_user_from_uid(uid)
20
+ flash[:notice] = 'Record created'#msg_created(user)
21
+ end
22
+ # render :js => %(window.location.href = '#{ucb_rails_admin_user_path}')
23
+ render :js => %(window.location.href = '#{edit_ucb_rails_admin_user_path(user)}')
24
+ end
25
+
26
+ def update
27
+ if @user.update_attributes(params.fetch(:ucb_rails_user), :without_protection => true)
28
+ redirect_to(ucb_rails_admin_users_path, notice: 'Record updated')
29
+ else
30
+ render("edit")
31
+ end
32
+ end
33
+
34
+ def destroy
35
+ if @user.destroy
36
+ flash[:notice] = 'Record deleted' #msg_destroyed(@user)
37
+ else
38
+ flash[:error] = @user.errors[:base].first
39
+ end
40
+
41
+ redirect_to(ucb_rails_admin_users_path)
42
+ end
43
+
44
+ def ldap_search
45
+ @lps_entries = UcbRails::LdapPerson::Finder.find_by_first_last(
46
+ params.fetch(:first_name),
47
+ params.fetch(:last_name),
48
+ :sort => :last_first_downcase
49
+ )
50
+ @lps_existing_uids = UcbRails::User.where(uid: @lps_entries.map(&:uid)).pluck(:uid)
51
+
52
+ render 'ucb_rails/lps/search'
53
+ end
54
+
55
+ def typeahead_search
56
+ tokens = params.fetch(:query).to_s.strip.split(/\s+/)
57
+ wheres = tokens.map { |e| 'first_last_name like ?' }.join(' and ')
58
+ values = tokens.map { |e| "%#{e}%" }
59
+ where = ["#{wheres}", *values]
60
+ results = UcbRails::User.where(*where).limit(10)
61
+ render json: results.map { |r| {uid: r.uid, first_last_name: r.first_last_name} }
62
+ end
63
+
64
+ private
65
+
66
+ def find_user
67
+ @user ||= UcbRails::User.find(params.fetch(:id))
68
+ end
69
+
70
+ end
@@ -0,0 +1,34 @@
1
+ class UcbRails::BootstrapController < ApplicationController
2
+ skip_before_filter :ensure_authenticated_user
3
+
4
+ def index
5
+ if RailsEnvironment.not_development?
6
+ raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"
7
+ end
8
+
9
+ if UcbRails::User.count > 0
10
+ return render_text("Can't bootstrap. Already have users.")
11
+ end
12
+
13
+ if uid.present?
14
+ UCB::LDAP::Person.find_by_uid(uid).tap do |e|
15
+ return render_text("Bad uid: #{uid.inspect}") if e.blank?
16
+ user = UcbRails::UserLdapService.create_user_from_uid(uid)
17
+ user.update_attributes({admin: true}, without_protection: true)
18
+ redirect_to login_path
19
+ end
20
+ else
21
+ return render_text "Must provide a uid: /ucb_rails/bootstrap/:uid"
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def uid
28
+ params[:uid]
29
+ end
30
+
31
+ def render_text(text)
32
+ render text: text
33
+ end
34
+ end
@@ -1,22 +1,83 @@
1
+ # Various controller methods mixed in to the host app.
2
+ #
3
+ # Most are also helper methods.
1
4
  module UcbRails::ControllerMethods
2
-
3
5
  extend ActiveSupport::Concern
4
6
 
5
- included do
6
- helper_method :current_ldap_person, :logged_in?
7
+ included do
8
+ rescue_from UcbRails::LdapPerson::Finder::BlankSearchTermsError do
9
+ render :js => %(alert("Enter search terms"))
7
10
  end
8
11
 
12
+ before_filter :ensure_authenticated_user
13
+ before_filter :log_request
14
+
15
+ after_filter :remove_user_settings
16
+
17
+ helper_method :admin?, :current_ldap_person, :current_user, :logged_in?
18
+ end
19
+
20
+ def admin?
21
+ current_user.try(:admin?)
22
+ end
23
+
24
+ def current_user
25
+ @current_user ||= begin
26
+ UcbRails.logger.debug 'recalc of current_user'
27
+ user_session_manager.current_user(session[:uid])
28
+ end
29
+ end
30
+
31
+ # Returns +true+ if there is a logged in user
32
+ #
33
+ # @return [true] if user logged in
34
+ # @return [false] if user not logged in
9
35
  def logged_in?
10
- session[:uid].present?
36
+ current_user.present?
37
+ end
38
+
39
+ def log_request
40
+ user_session_manager.log_request(current_user)
11
41
  end
12
42
 
43
+ def remove_user_settings
44
+ end
45
+
46
+ # Returns an instance of UCB::LDAP::Person if there is a logged in user
47
+ #
48
+ # @return [UCB::LDAP::Person] if user logged in
49
+ # @return [nil] if user not logged in
13
50
  def current_ldap_person
14
51
  if logged_in?
15
- UCB::LDAP::Person.find_by_uid(session[:uid])
52
+ @current_ldap_person ||= begin
53
+ UcbRails.logger.debug 'recalc of current_ldap_person'
54
+ user_session_manager.people_ou_entry(session[:uid])
55
+ end
56
+ end
57
+ end
58
+
59
+ def ensure_admin_user
60
+ admin? or redirect_to not_authorized_path
61
+ end
62
+
63
+ # Before filter that redirects redirects to +login_url+ unless user is logged in
64
+ #
65
+ # @return [nil]
66
+ def ensure_authenticated_user
67
+ unless session.has_key?(:uid)
68
+ session[:original_url] = request.env['REQUEST_URI']
69
+ redirect_to login_url
16
70
  end
17
71
  end
18
72
 
19
- module ClassMethods
73
+ def user_session_manager
74
+ @user_session_manager ||= begin
75
+ UcbRails.logger.debug "creating new user_session_manager"
76
+ klass = UcbRails[:user_session_manager] || UcbRails::UserSessionManager::ActiveInUserTable
77
+ klass.to_s.classify.constantize.new
78
+ end
79
+ rescue NameError
80
+ raise "Could not find UcbRails user_session_manager: #{klass}"
20
81
  end
21
82
 
22
83
  end
@@ -1,5 +1,10 @@
1
1
  class UcbRails::HomeController < ApplicationController
2
2
 
3
+ # Renders either a "logged in" page or a "logged out" page.
4
+ # Mainly used for getting going with the +ucb_rails+ gem.
5
+ # Not intended for production use.
6
+ #
7
+ # @return [nil]
3
8
  def index
4
9
  if logged_in?
5
10
  render 'logged_in'
@@ -0,0 +1,15 @@
1
+ class UcbRails::LdapPersonSearchController < ApplicationController
2
+ NoSearchTermError = Class.new(StandardError)
3
+
4
+ skip_before_filter :ensure_authenticated_user
5
+
6
+ def search
7
+ @lps_entries = UcbRails::LdapPerson::Finder.find_by_first_last(
8
+ params.fetch(:first_name),
9
+ params.fetch(:last_name),
10
+ :sort => :last_first_downcase
11
+ )
12
+ render 'ucb_rails/lps/search'
13
+ end
14
+
15
+ end
@@ -1,26 +1,51 @@
1
+ # Manages starting and ending of sessions, i.e., logging in and out.
1
2
  class UcbRails::SessionsController < ApplicationController
2
3
 
3
- skip_before_filter :ensure_authenticated_user
4
+ skip_before_filter :ensure_authenticated_user, :log_request
4
5
 
6
+ # Redirects to authentication provider
7
+ #
8
+ # @return [nil]
5
9
  def new
6
- provider = params.fetch(:provider, :cas)
10
+ provider = UcbRails[:omniauth_provider] || :cas
7
11
  redirect_to "/auth/#{provider}"
8
12
  end
9
13
 
14
+ # Login user after authentication by provider
15
+ #
16
+ # @return [nil]
10
17
  def create
11
- session[:uid] = request.env['omniauth.auth'].uid
12
- session[:provider] = request.env['omniauth.auth'].provider
13
- # UserSessionManager.login(session[:uid])
14
- redirect_to session[:original_url] || root_path
18
+ uid = request.env['omniauth.auth'].uid
19
+ session[:omniauth_provider] = request.env['omniauth.auth'].provider
20
+
21
+ if user_session_manager.login(uid)
22
+ session[:uid] = uid
23
+ redirect_to session[:original_url] || root_path
24
+ else
25
+ redirect_to not_authorized_path
26
+ end
15
27
  end
16
28
 
29
+ # Log user out
30
+ #
31
+ # @return [nil]
17
32
  def destroy
18
- # UserSessionManager.logout(current_user)
19
- provider = session[:provider]
33
+ user_session_manager.logout(current_user)
34
+ provider = session[:omniauth_provider]
20
35
  reset_session
21
36
  redirect_to redirect_url(provider)
22
37
  end
23
38
 
39
+ # Action called when unauthorized access attempted
40
+ #
41
+ # @return [nil]
42
+ def not_authorized
43
+ render(:text => "Not Authorized", :status => 401)
44
+ end
45
+
46
+ # Handler for authentication failure.
47
+ #
48
+ # @return [nil]
24
49
  def failure
25
50
  Rails.logger.debug("Authentication Failed for: #{request.env['omniauth.auth']}")
26
51
  render(:text => "Not Authorized", :status => 401)
@@ -0,0 +1,14 @@
1
+ module UcbRails::Admin::UsersHelper
2
+
3
+ def link_to_new_user
4
+ button('New User', :primary,
5
+ class: 'ldap-person-search',
6
+ data: {
7
+ search_url: ldap_search_ucb_rails_admin_users_path,
8
+ result_link_url: ucb_rails_admin_users_path,
9
+ result_link_http_method: 'post',
10
+ }
11
+ )
12
+ end
13
+
14
+ end
@@ -0,0 +1,17 @@
1
+ # These methods should be extracted into own gem.
2
+ module UcbRails::ExtractableHelper
3
+
4
+ def ucbr_table_tag(*args)
5
+ options = canonicalize_options(args.extract_options!)
6
+
7
+ ar_class = args.first
8
+ if ar_class.respond_to?(:haml_attributes)
9
+ options[:id] ||= ar_class.haml_attributes["id"]
10
+ options = ensure_class(options, ar_class.haml_attributes["class"])
11
+ end
12
+
13
+ bs_table_tag(options) do
14
+ yield
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ module UcbRails::IconsHelper
2
+
3
+ def announcements_text
4
+ icon('bullhorn', 'Announcements')
5
+ end
6
+
7
+ end
@@ -0,0 +1,21 @@
1
+ module UcbRails::LdapPersonSearchHelper
2
+
3
+ def link_to_ldap_person_entry(entry)
4
+ UcbRails::Renderer::LdapPersonSearchResultLink.new(self, entry).html
5
+ end
6
+
7
+ def ldap_entry_class(entry)
8
+ Array['ldap-result'].tap do |result|
9
+ result << 'ldap-result-exists' if ldap_already_include?(entry.uid)
10
+ end
11
+ end
12
+
13
+ def ldap_already_include?(uid)
14
+ ldap_person_search_existing_uids.include?(uid.to_s)
15
+ end
16
+
17
+ def ldap_person_search_existing_uids
18
+ @ldap_person_search_existing_uids ||= (@lps_existing_uids || []).map(&:to_s)
19
+ end
20
+
21
+ end
@@ -0,0 +1,50 @@
1
+ module UcbRails::LpsTypeaheadHelper
2
+
3
+ def lps_typeahead_search_field(options={})
4
+ name = options.delete(:name) || 'person_search'
5
+ required = options.delete(:required)
6
+ label = options.delete(:label) || 'User'
7
+ value = options.delete(:value) || params[name]
8
+ placeholder = options.delete(:placeholder) || 'Type name to search'
9
+ hint = options.delete(:hint) || 'Click icon to search CalNet'
10
+
11
+ result_link_text = options.delete(:result_link_text) || 'Select'
12
+ result_link_class = options.delete(:result_link_class) || 'lps-typeahead-item'
13
+
14
+ uid_dom_id = options.delete(:uid_dom_id) || 'uid'
15
+ search_url = options.delete(:search_url) || typeahead_search_ucb_rails_admin_users_path
16
+
17
+ content_tag(:div, class: 'control-group lps-typeahead') do
18
+ label_tag(name, class: label_classes(required)) do
19
+ required_marker(required) +
20
+ label
21
+
22
+ end +
23
+ content_tag(:div, class: 'controls') do
24
+ content_tag(:div, class: 'input-append') do
25
+ text_field_tag(name, value, placeholder: placeholder, class: 'typeahead-lps-search', data: { uid_dom_id: uid_dom_id, url: search_url }) +
26
+ content_tag(:span, class: 'add-on') do
27
+ icon('search', class: 'ldap-person-search', data: {search_field_name: name, result_link_text: result_link_text, result_link_class: result_link_class})
28
+ end
29
+ end +
30
+ content_tag(:p, hint, class: 'help-block')
31
+ end
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def label_classes(required)
38
+ Array['control-label'].tap do |klasses|
39
+ klasses << 'required' if required
40
+ end
41
+ end
42
+
43
+ def required_marker(required)
44
+ if required
45
+ content_tag(:abbr, '*', title: 'required') + ' '
46
+ else
47
+ ''
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,52 @@
1
+ # Base class for helper objects inspired by http://railscasts.com/episodes/101-refactoring-out-helper-object
2
+ #
3
+ # Use of such classes is indicated when you have:
4
+ #
5
+ # * helper methods calling each other, especially if ...
6
+ # * helper methods are passing around variables (which will become instance variables)
7
+ # * the same case/if logic in many methods, indicating a requirement for class hierarchy
8
+ #
9
+ # Your helper class's methods can just do calculations which are subsituted into
10
+ # html in the views, or the methods can generate html as well. This is helpful
11
+ # when the html is conditional on some other object's state.
12
+ #
13
+ # You have access to all the helper methods available to the view that creates
14
+ # the instance.
15
+ #
16
+ # Implementing helper classes should inherit from Renderer::Base and the template
17
+ # instance should be passed as an argument to the constructor.
18
+ #
19
+ # # app/helpers/renderer/my_renderer.rb
20
+ # class MyRenderer < Renderer::Base
21
+ # ...
22
+ # end
23
+ #
24
+ # # app/views/a_controller/a_view.html.erb
25
+ # <% renderer = MyRenderer.new(self) %> # must pass in template
26
+ # ...
27
+ # <div>
28
+ # <%= renderer.div_content %>
29
+ # </div>
30
+ class UcbRails::Renderer::Base
31
+
32
+ # Holds reference to view template
33
+ attr_accessor :template
34
+
35
+ # Options to pass to renderer
36
+ attr_accessor :options
37
+
38
+ # Returns new instance of renderer. The _template_ argument is the view template.
39
+ def initialize(template, options = {})
40
+ self.template = template
41
+ self.options = options
42
+ end
43
+
44
+ private
45
+
46
+ # Method missing sends all calls to template instance. This techniuqe
47
+ # provides access to helper methods available to the template.
48
+ def method_missing(*args, &block)
49
+ template.send(*args, &block)
50
+ end
51
+
52
+ end
@@ -0,0 +1,81 @@
1
+ module UcbRails
2
+ module Renderer
3
+
4
+ class LdapPersonSearchResultLink < Base
5
+ attr_accessor :entry
6
+
7
+ def initialize(template, entry, options={})
8
+ super template, options
9
+ self.entry = entry
10
+ end
11
+
12
+ def html
13
+ exists? ? exists_html : button(text, :mini, :primary, link_options)
14
+ end
15
+
16
+ private
17
+
18
+ def exists?
19
+ ldap_person_search_existing_uids.include?(entry.uid.to_s)
20
+ end
21
+
22
+ def exists_html
23
+ params["result-link-exists-text"] || 'Exists'
24
+ end
25
+
26
+ def text
27
+ @text ||= params["result-link-text"].presence || "Add"
28
+ end
29
+
30
+ def link_options
31
+ {
32
+ class: classes,
33
+ url: item_url,
34
+ data: {
35
+ remote: remote,
36
+ method: http_method,
37
+ uid: entry.uid,
38
+ first_name: entry.first_name,
39
+ last_name: entry.last_name,
40
+ email: entry.email,
41
+ }
42
+ }
43
+ end
44
+
45
+ def classes
46
+ extra_class = params['result-link-class'].presence || 'result-link-default'
47
+ "result-link #{extra_class}"
48
+ end
49
+
50
+ def item_url
51
+ url_param.present? ? url_and_person_params : '#'
52
+ end
53
+
54
+ def remote
55
+ url_param.present?
56
+ end
57
+
58
+ def http_method
59
+ params['result-link-http-method'] || :get
60
+ end
61
+
62
+ def url_param
63
+ params["result-link-url"]
64
+ end
65
+
66
+ def url_and_person_params
67
+ connector = url_param.include?("?") ? '&' : '?'
68
+ "#{url_param}#{connector}#{person_params.to_query}"
69
+ end
70
+
71
+ def person_params
72
+ {
73
+ uid: entry.uid,
74
+ first_name: entry.first_name,
75
+ last_name: entry.last_name,
76
+ }
77
+ end
78
+ end
79
+
80
+ end
81
+ end
@@ -0,0 +1,54 @@
1
+ module UcbRails::LdapPerson
2
+ class Entry
3
+
4
+ # include ActiveAttr::Model <--- doesn't work with 4.0 (mass assign. security)
5
+ include ::ActiveAttr::Attributes
6
+ include ::ActiveAttr::BasicModel
7
+ include ::ActiveAttr::BlockInitialization
8
+ include ::ActiveAttr::MassAssignment
9
+
10
+ attribute :uid
11
+ attribute :calnet_id
12
+ attribute :first_name
13
+ attribute :last_name
14
+ attribute :email
15
+ attribute :phone
16
+ attribute :departments
17
+ attribute :affiliations
18
+
19
+ def full_name
20
+ "#{first_name()} #{last_name()}"
21
+ end
22
+
23
+ def last_first
24
+ "#{last_name}, #{first_name}"
25
+ end
26
+
27
+ def last_first_downcase
28
+ last_first.downcase
29
+ end
30
+
31
+ # Currently only used in rspec
32
+ def ==(other)
33
+ uid == other.uid
34
+ end
35
+
36
+ class << self
37
+
38
+ def new_from_ldap_entry(ldap_entry)
39
+ new(
40
+ :uid => ldap_entry.uid,
41
+ :calnet_id => ldap_entry.berkeleyedukerberosprincipalstring.first,
42
+ :first_name => ldap_entry.givenname.first,
43
+ :last_name => ldap_entry.sn.first,
44
+ :email => ldap_entry.mail.first,
45
+ :phone => ldap_entry.phone,
46
+ :departments => ldap_entry.berkeleyeduunithrdeptname,
47
+ :affiliations => ldap_entry.berkeleyeduaffiliations
48
+ )
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end