Skip to content

Instantly share code, notes, and snippets.

@jkyamog
Created May 21, 2013 02:32
Show Gist options
  • Save jkyamog/5617158 to your computer and use it in GitHub Desktop.
Save jkyamog/5617158 to your computer and use it in GitHub Desktop.
deadbolt-2 using ldap
// controller action
def create = SecuredAction(Seq("roleA"), parse.json) { implicit request =>
}
def ldapFind = Action { request =>
val results = LdapPlugin.filter("userPrincipalName", "foo", "dc=foo,dc=com", "cn")
Ok(results.map{ r => r.getAttributeValue("cn")}.mkString)
}
object SecuredAction extends DeadboltActions {
def apply[T](restrictRoles: Seq[String], bodyParser: BodyParser[T])(code: Request[T] => Result) = Restrict(restrictRoles.toArray, new OnestoreDeadboltHandler) {
Action(bodyParser) { implicit request =>
code(request)
}
}
}
// ldap plugin using unboundedid
package play.modules.ldap
import play.api._
import com.unboundid.ldap.sdk._
import scala.collection.JavaConversions._
import scala.concurrent.ExecutionContext
class LdapPlugin(app: Application) extends Plugin {
lazy val host = app.configuration.getString("ldap.host")
lazy val port = app.configuration.getInt("ldap.port")
lazy val bindDn = app.configuration.getString("ldap.bindDn")
lazy val password = app.configuration.getString("ldap.password")
lazy val connection = {
if (host.isDefined && bindDn.isDefined && password.isDefined)
new LDAPConnection(host.get, port.getOrElse(389), bindDn.get, password.get)
else
throw new PlayException("LdapPlugin Initialization Error", s"ldap.host = ${host}, ldap.bindDn = ${bindDn} and ldap.password = ${password} are required configs")
}
override lazy val enabled = {
!app.configuration.getString("ldapplugin").filter(_ == "disabled").isDefined
}
override def onStart {
connection
}
override def onStop {
connection.close
}
}
object LdapPlugin extends LdapOperations
trait LdapOperations {
def filter(attributeName: String, attributeValue: String, baseDn: String, resultAttributes: String*)(implicit app: Application, ec: ExecutionContext) = {
val filter = Filter.createEqualityFilter(attributeName, attributeValue)
val searchRequest = new SearchRequest(baseDn, SearchScope.SUB, filter, resultAttributes: _*)
for {
entry <- current.connection.search(searchRequest).getSearchEntries
} yield (entry)
}
def current(implicit app: Application): LdapPlugin = app.plugin[LdapPlugin] match {
case Some(plugin) => plugin
case _ => throw new PlayException("LdapPlugin Error", "The LdapPlugin has not been initialized! Please edit your conf/play.plugins file and add the following line: '1000:play.modules.ldap.LdapPlugin' (1000 is an arbitrary priority and may be changed to match your needs).")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment