[PATCH conductor 4/4] task 3533: LDAP group support

Scott Seago sseago at redhat.com
Wed Jul 25 21:39:04 UTC 2012


Update SessionEntities to include ldap groups as well as local groups.
---
 src/app/controllers/user_groups_controller.rb |    2 --
 src/app/models/user.rb                        |   23 +++++++++++++++++++++--
 src/app/models/user_group.rb                  |   16 ++++++++++++++++
 src/app/views/user_groups/_form.html.haml     |    3 +--
 src/app/views/user_groups/_list.html.haml     |    2 +-
 src/app/views/user_groups/show.html.haml      |    2 +-
 src/config/settings.yml                       |    5 +++++
 7 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/src/app/controllers/user_groups_controller.rb b/src/app/controllers/user_groups_controller.rb
index 59b8665..6cae0fa 100644
--- a/src/app/controllers/user_groups_controller.rb
+++ b/src/app/controllers/user_groups_controller.rb
@@ -55,8 +55,6 @@ class UserGroupsController < ApplicationController
     require_privilege(Privilege::CREATE, User)
     @title = t'user_groups.new.new_user_group'
     @user_group = UserGroup.new
-    # remove when we enable ldap
-    @user_group.membership_source = UserGroup::MEMBERSHIP_SOURCE_LOCAL
   end
 
   def create
diff --git a/src/app/models/user.rb b/src/app/models/user.rb
index 892c62c..73579e5 100644
--- a/src/app/models/user.rb
+++ b/src/app/models/user.rb
@@ -142,8 +142,27 @@ class User < ActiveRecord::Base
   PRESET_FILTERS_OPTIONS = []
 
   def all_groups
-    self.user_groups
-    # pull and create LDAP groups here too
+    group_list = []
+    group_list += self.user_groups if UserGroup.local_groups_active?
+    if UserGroup.ldap_groups_active?
+      ldap_group_names = Ldap.ldap_groups(self.login)
+      ldap_group_names.each do |group_name|
+        ldap_group = UserGroup.find_by_name_and_membership_source(
+            group_name, UserGroup::MEMBERSHIP_SOURCE_LDAP)
+        if ldap_group
+          # update  group on each login so we can later check/purge groups
+          # that haven't been updated lately (i.e. no recent logins by users
+          # that belong to them)
+          ldap_group.touch
+        else
+          ldap_group = UserGroup.create!(:name => group_name,
+                                         :membership_source =>
+                                           UserGroup::MEMBERSHIP_SOURCE_LDAP)
+        end
+        group_list << ldap_group
+      end
+    end
+    group_list
   end
 
   private
diff --git a/src/app/models/user_group.rb b/src/app/models/user_group.rb
index dcb2f63..3d4513d 100644
--- a/src/app/models/user_group.rb
+++ b/src/app/models/user_group.rb
@@ -46,6 +46,22 @@ class UserGroup < ActiveRecord::Base
     self.entity.save!
   end
 
+  def self.local_groups_active?
+    SETTINGS_CONFIG[:groups][:enable_local]
+  end
+
+  def self.ldap_groups_active?
+    SETTINGS_CONFIG[:groups][:enable_ldap] &&
+        SETTINGS_CONFIG[:auth][:strategy] == "ldap"
+  end
+
+  def self.active_membership_sources
+    sources = []
+    sources << MEMBERSHIP_SOURCE_LOCAL if self.local_groups_active?
+    sources << MEMBERSHIP_SOURCE_LDAP if self.ldap_groups_active?
+    sources
+  end
+
   def self.apply_search_filter(search)
     if search
       where("lower(name) LIKE :search", :search => "%#{search.downcase}%")
diff --git a/src/app/views/user_groups/_form.html.haml b/src/app/views/user_groups/_form.html.haml
index 1db6a7a..9fe8169 100644
--- a/src/app/views/user_groups/_form.html.haml
+++ b/src/app/views/user_groups/_form.html.haml
@@ -6,8 +6,7 @@
     = form.label :type
     .input
       - if @user_group.new_record?
-        = form.select(:membership_source, UserGroup::MEMBERSHIP_SOURCES.collect {|x| [t("user_groups.#{x.downcase}"), x]}, {}, {:disabled => true})
-        = form.hidden_field :membership_source
+        = form.select(:membership_source, UserGroup.active_membership_sources.collect {|x| [t("user_groups.#{x.downcase}"), x]}, {})
       - else
         = t("user_groups.#{@user_group.membership_source.downcase}")
   .field
diff --git a/src/app/views/user_groups/_list.html.haml b/src/app/views/user_groups/_list.html.haml
index 5c99b39..0f6d2f3 100644
--- a/src/app/views/user_groups/_list.html.haml
+++ b/src/app/views/user_groups/_list.html.haml
@@ -3,7 +3,7 @@
 
 - content_for :form_header do
   %li= restful_submit_tag "#{t'user_groups.list.delete_selected'}", "destroy", multi_destroy_user_groups_path, 'DELETE', :id => 'delete_button', :class => 'button danger'
-  %li= link_to t("user_groups.list.add_user_group"), new_user_group_path, :class => 'button', :id => 'add_user_group_button'
+  %li= link_to t("user_groups.list.add_user_group"), new_user_group_path, :class => 'button', :id => 'add_user_group_button' unless UserGroup.active_membership_sources.nil?
 
 - content_for :filter_controls do
   %li
diff --git a/src/app/views/user_groups/show.html.haml b/src/app/views/user_groups/show.html.haml
index 426137f..cb22930 100644
--- a/src/app/views/user_groups/show.html.haml
+++ b/src/app/views/user_groups/show.html.haml
@@ -5,7 +5,7 @@
       = t'return_to'
       = link_to t("user_groups.groups"), user_groups_path
     - if check_privilege(Privilege::CREATE, User)
-      = link_to t('user_groups.new.new_user_group'), new_user_group_url, :class => 'button primary', :id => 'new_user_group_button'
+      = link_to t('user_groups.new.new_user_group'), new_user_group_url, :class => 'button primary', :id => 'new_user_group_button' unless UserGroup.active_membership_sources.nil?
     .button-group
       - if check_privilege(Privilege::MODIFY, User)
         = link_to t('edit'), edit_user_group_path(@user_group), :class => 'button pill', :id => 'edit_button'
diff --git a/src/config/settings.yml b/src/config/settings.yml
index bf1430a..eab8a58 100644
--- a/src/config/settings.yml
+++ b/src/config/settings.yml
@@ -3,6 +3,11 @@
 :auth:
   # supported strategies: database, ldap
   :strategy: database
+:groups:
+  # allows locally-managed groups
+  :enable_local: true
+  # allows ldap-managed groups
+  :enable_ldap: true
 :iwhd:
   :url: http://localhost:9090
   # :oauth:
-- 
1.7.6.5




More information about the aeolus-devel mailing list