Lift-LDAP module updated

lift-ldap
sample_lift_ldap

Some code cleanup …
Now it’s easier to create the user model object and customize it.

  • The LDAPProtoUser defines now the ldapRoles SessionVar to get the user roles or groups.
    Also defines the rolesSearchFilter and rolesNameRegex to search and get the roles/groups.

    
    trait LDAPProtoUser[T <: LDAPProtoUser[T]] extends MegaProtoUser[T] {
        self: T =<
        /**
         * User Roles LDAP search filter
         */
        def rolesSearchFilter: String = "(&(objectclass=groupofnames)(member=%s))"
    
        /**
         * Regular expression to get user roles names
         */
        def rolesNameRegex = ".*cn=(.[^,]*),ou=.*"
    
        object ldapRoles extends SessionVar[List[String]](List())
    
        override def getSingleton: MetaLDAPProtoUser[T]
    
        object uid extends MappedString(this, 64) {
            override def dbIndexed_? = true
        }
    
        object dn extends MappedString(this, 64) {
            override def dbIndexed_? = true
        }
    
        object cn extends MappedString(this, 64) {
            override def dbIndexed_? = true
        }
    
        def getRoles: List[String] = {
            return ldapRoles.get
        }
    
        def setRoles(userDn: String, ldapVendor: LDAPVendor): AnyRef = {
            def getGroupNameFromDn(dn: String): String = {
                val regex = new Regex(rolesNameRegex)
    
                val regex(groupName) = dn
                return groupName
            }
    
            // Search for user roles
            val filter = rolesSearchFilter.format(userDn)
    
            val groups = ldapVendor.search(filter)
            groups.foreach(g => {
                ldapRoles.set(ldapRoles.get + getGroupNameFromDn(g))
            })
        }
    }
    
    
    
  • The user model object only has to redefine the roles search (the ldap search sentence or implement a setRoles function to realize the custom search)

    
    class User extends LDAPProtoUser[User] {
        def getSingleton = User
    }
    
    object User extends User with MetaLDAPProtoUser[User] {
        override def screenWrap = Full(<lift:surround with="default" at="content">
                       <lift:bind />
        </lift:surround>)
    }
    
    

    Overriding default values …

    
    object User extends User with MetaLDAPProtoUser[User] {
        override def loginErrorMessage: String = "'%s' is not a valid user or password does not match"
        override def ldapUserSearch: String = "(&(objectClass=inetOrgPerson)(uid=%s))"
    
        override def rolesNameRegex: String = ".*cn=(.[^,]*),.*"
        override def rolesSearchFilter: String = "(&(objectclass=groupofnames)(!(cancellationdate=*))(member=%s))"
    
        override def screenWrap = Full(<lift:surround with="default" at="content">
                       <lift:bind />
        </lift:surround>)
    }
    

Now have to remove some unused imports 🙂

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: