Browse Source

Moved to modular auth backend layers. Still config options are needed. Related to #58.

Martin Edenhofer 12 years ago
parent
commit
83290f4953
6 changed files with 135 additions and 15 deletions
  1. 37 15
      app/models/user.rb
  2. 17 0
      lib/auth/env.rb
  3. 15 0
      lib/auth/internal.rb
  4. 13 0
      lib/auth/ldap.rb
  5. 42 0
      lib/auth/otrs.rb
  6. 11 0
      lib/auth/test.rb

+ 37 - 15
app/models/user.rb

@@ -53,21 +53,43 @@ class User < ApplicationModel
     # no user found
     return nil if !user
 
-    # development systems
-    if !ENV['RAILS_ENV'] || ENV['RAILS_ENV'] == 'development'
-      if password == 'test'
-        return user
-      end
-    end
-
-    # sha auth check
-    if user.password =~ /^\{sha2\}/
-      crypted = Digest::SHA2.hexdigest( password )
-      return user if user.password == "{sha2}#{crypted}"
-    end
-
-    # plain auth check
-    return user if user.password == password
+    # use auth backends
+    config = {
+      :internal => {
+        :adapter => 'internal',
+      },
+      :test => {
+        :adapter => 'test',
+      },
+      :env => {
+        :adapter => 'env',
+      },
+      :ldap => {
+        :adapter    => 'ldap',
+        :host       => 'somehost',
+        :port       => '3333',
+        :base_dn    => 'some base dn',
+        :bind_user  => 'some bind user',
+        :bind_pw    => 'some pw',
+      },
+      :otrs => {
+        :adapter        => 'otrs',
+        :required_group => 'stats',
+        :group_role_map => {
+          'admin' => 'Admin',
+          'stats' => 'Report',
+        },
+        :always_role    => {
+          'Agent' => true,
+        },
+      },
+    }
+    config.each {|key, c|
+      file = "auth/#{c[:adapter]}"
+      require file
+      user_auth = Auth.const_get("#{c[:adapter].to_s.upcase}").check( user, username, password, c )
+      return user_auth if user_auth
+    }
 
     # auth failed
     return false

+ 17 - 0
lib/auth/env.rb

@@ -0,0 +1,17 @@
+module Auth::ENV
+  def self.check( user, username, password, config )
+
+    # try to find user based on login
+    if ENV['REMOTE_USER']
+      user = User.where( :login => ENV['REMOTE_USER'], :active => true ).first
+      return user if user
+    end
+
+    if ENV['HTTP_REMOTE_USER']
+      user = User.where( :login => ENV['HTTP_REMOTE_USER'], :active => true ).first
+      return user if user
+    end
+
+    return false
+  end
+end

+ 15 - 0
lib/auth/internal.rb

@@ -0,0 +1,15 @@
+module Auth::INTERNAL
+  def self.check( user, username, password, config )
+    
+    # sha auth check
+    if user.password =~ /^\{sha2\}/
+      crypted = Digest::SHA2.hexdigest( password )
+      return user if user.password == "{sha2}#{crypted}"
+    end
+
+    # plain auth check
+    return user if user.password == password
+
+    return false
+  end
+end

+ 13 - 0
lib/auth/ldap.rb

@@ -0,0 +1,13 @@
+module Auth::LDAP
+  def self.check( user, username, password, config )
+    
+    # ldap connect
+    
+    # ldap bind
+    
+    # sync roles / groups
+#    return user
+
+    return false
+  end
+end

+ 42 - 0
lib/auth/otrs.rb

@@ -0,0 +1,42 @@
+class Auth::OTRS
+  def self.check( user, username, password, config )
+
+    # connect to OTRS
+    result = Import::OTRS.auth( username, password )
+    return false if !result
+    return false if !result['groups_rw']
+
+    # check if required OTRS group exists
+    return false if !result['groups_rw'].has_value?( config[:required_group] )
+
+    # sync roles / groups
+    if config[:group_role_map]
+      config[:group_role_map].each {|otrs_group, role|
+        if result['groups_rw'].has_value?( otrs_group )
+          role_ids = user.role_ids
+          role = Role.where( :name => role ).first
+          if role
+            role_ids.push role.id
+            user.role_ids = role_ids
+            user.save
+          end
+        end
+      }
+    end
+
+    if config[:always_role]
+      config[:always_role].each {|role, active|
+        next if !active
+        role_ids = user.role_ids
+        role = Role.where( :name => role ).first
+        if role
+          role_ids.push role.id
+          user.role_ids = role_ids
+          user.save
+        end
+      }
+    end
+
+    return user
+  end
+end

+ 11 - 0
lib/auth/test.rb

@@ -0,0 +1,11 @@
+module Auth::TEST
+  def self.check( user, username, password, config )
+
+    # development systems
+    if !ENV['RAILS_ENV'] || ENV['RAILS_ENV'] == 'development'
+      return user if password == 'test'
+    end
+    
+    return false
+  end
+end