Jump to content


Jim Yost

Member Since 27 Dec 2010
Offline Last Active Mar 10 2012 11:54 AM
-----

Posts I've Made

In Topic: LDAP Auth for Session login

18 February 2012 - 02:41 PM

Thanks WillyC, that is exactly what I was looking for!

The module still needs work with ldap settings (specifically TLS and other connection types that LDAP works with). I just needed it to show my company how flexible ProcessWire is :-).

Oliver, let me get a formal module together that supports more complete features of LDAP before you grab it (though you are welcome to do so). I started one for looking up users, groups, and pulling information as well and will end up tieing the two together.

-J

In Topic: LDAP Auth for Session login

17 February 2012 - 05:01 PM


<?php

class LdapAuth extends WireData implements Module, ConfigurableModule {

public static function getModuleInfo() {

  return array(

   "title" => "LDAP Authentication",

   "version" => 100,

   "summary" => "Allows uses to be authenticated via LDAP",

   "permanent" => false,

   "singular" => true,

   "autoload" => true

  );

}



public function init() {

  $this->session->addHookAfter('login', $this, 'login');

}



public function ___login($event) {

  if ($event->return) return; // they are already in

 

  $name = $event->arguments[0];

  $pass = $event->arguments[1];

 

  $conn = ldap_connect($this->data['host']);

  if ($conn) {

   $bind = @ldap_bind($conn, "$name@{$this->data['accountDomainName']}", $pass);

  

   if ($bind) {

    // success

    // check if they area lready a user in PW

    $user = wire('users')->get("name=$name");

    if (!$user instanceof NullPage) {

	 // update login info

	 $user->pass = $pass;

	 $user->save();

	

	 $user = wire('session')->login($name, $pass);

	 $event->return = $user;

	 return;

    } else {

	 // create a new user

	 $guest = wire('users')->getGuestUser();

	 $user = new User();

	 $user->parent = $guest->parent;

	 $user->name = $name;

	 $user->pass = $pass;

	 $user->addRole("guest");

	 $user->save();

	

	 $user = wire('session')->login($name, $pass);

	 $event->return = $user;

	 return;

    }

   } else {

    // fail

    $event->return = null;

    return;

   }

  } else {

   // could not connect

   throw new Exception("Could not connect to LDAP");

  }

}



static public function getModuleConfigInputfields(array $data) {

  $inputfields = new InputfieldWrapper();

 

  $field = Wire::getFuel('modules')->get('InputfieldText');

  $field->attr('name', 'host');

  $field->label = 'Host';

  if (isset($data['host'])) $field->attr('value', $data['host']);

  $field->description = 'The LDAP server hostname';

  $inputfields->append($field);

 

  $field = Wire::getFuel('modules')->get('InputfieldText');

  $field->attr('name', 'accountDomainName');

  $field->label = 'Account Domain Name';

  if (isset($data['accountDomainName'])) $field->attr('value', $data['accountDomainName']);

  $field->description = 'The LDAP server domain';

  $inputfields->append($field);

 

  $field = Wire::getFuel('modules')->get('InputfieldText');

  $field->attr('name', 'accountDomainNameShort');

  $field->label = 'LDAP server domain (short)';

  if (isset($data['accountDomainNameShort'])) $field->attr('value', $data['accountDomainNameShort']);

  $field->description = 'The LDAP server hostname';

  $inputfields->append($field);

 

  $field = Wire::getFuel('modules')->get('InputfieldText');

  $field->attr('name', 'baseDn');

  $field->label = 'Base DN';

  if (isset($data['baseDn'])) $field->attr('value', $data['baseDn']);

  $field->description = 'The LDAP server DN';

  $inputfields->append($field);

 

  $field = Wire::getFuel('modules')->get('InputfieldCheckbox');

  $field->attr('name', 'startTls');

  $field->label = 'Use TLS';

  $field->attr('value', 1);

  if (isset($data['startTls'])) {

   if ($data['startTls']) $field->attr('checked', true);

  }

  $field->description = 'Check this option to enable TLS security';

  $inputfields->append($field);

  return $inputfields;

}

}


In Topic: Using $page->getArray()

08 April 2011 - 01:44 PM

Perfect, thanks.

In Topic: Make session_start() in Session overridable from a module

22 March 2011 - 03:56 PM

Hey Ryan,
That works well for me, I was just thinking it would be nice to package it all up into a single module and not worry about putting code elsewhere. I also realize this is an extremely rare situation so it's not a big deal for now.

-Jim

In Topic: ProcessTemplate Additions

28 February 2011 - 10:46 PM

Thanks Ryan,
I tried adding the $config->loginPageID = $pages->get("/it/login/")->id; to my controller and it didn't help. I was still re-directed to the /processwire/login/ I believe that PW redirects before it even gives control to the template or controller. I only want the /it/login/ to show for the /it/ branch of templates.

This isn't a big deal for now, I currently have /it/ guest accessible and it will redirect to /it/login/ if the user isn't logged in or /it/dashboard/ if the user is logged in.

-Jim