. * */ class Staple_ADAuthAdapter implements Staple_AuthAdapter { /** * * Enter description here ... * @var array */ private $_settings = array(); /** * * Store the user identifier. Usually the username. * @var string */ private $uid; /** * * The constructor loads and checks the adapter configuration. * @throws Exception */ public function __construct() { if(file_exists(CONFIG_ROOT.'application.ini')) { $curConfig = parse_ini_file(CONFIG_ROOT.'application.ini',true); if($this->checkConfig($curConfig['auth'])) { $this->_settings = $curConfig['auth']; } } elseif(file_exists(CONFIG_ROOT.'auth.ini')) { $curConfig = parse_ini_file(CONFIG_ROOT.'auth.ini'); if($this->checkConfig($curConfig)) { $this->_settings = $curConfig; } } else { throw new Exception('Staple_ADAuthAdapter critical failure.',500); } } /** * getAuth checks Active Directory for valid credentials and returns true if they are found. * @param array $cred * @return bool * @see Staple_AuthAdapter::getAuth() */ public function getAuth($cred) { if($this->checkConfig($this->_settings)) { if(array_key_exists('username', $cred) AND array_key_exists('password', $cred)) { if(strlen($cred['username']) >= 1 && strlen($cred['password']) >= 1) { if(Staple_AD::validchars($cred['username']) == TRUE && Staple_AD::validchars($cred['password']) == TRUE) { $pass = $cred['password']; $LDAP = Staple_AD::get(); $this->uid = $cred['username']; if($LDAP->bind($this->uid, $pass)) { return true; } } } } } return false; } /** * Gets the access level for the supplied $uid. * @param string $uid * @return int * @see Staple_AuthAdapter::getLevel() */ public function getLevel($uid) { if($this->checkConfig($this->_settings)) { if(array_key_exists('rolefield', $this->_settings)) { $db = Staple_DB::get(); $sql = 'SELECT '.$db->real_escape_string($this->_settings['rolefield']).' FROM '.$db->real_escape_string($this->_settings['authtable']).' WHERE '.$db->real_escape_string($this->_settings['uidfield']).' = '. '\''.$db->real_escape_string($uid).'\';'; $result = $db->query($sql); if($result !== false) { $myrow = $result->fetch_array(); $level = (int)$myrow[$this->_settings['rolefield']]; if($level < 0) { return 0; } else { return $level; } } else { return 0; } } else { return 1; } } } /** * * Checks the configuration fields for validity * @param array $config * @throws Exception */ protected function checkConfig(array $config) { $keys = array('enabled','adapter'); foreach($keys as $value) { if(!array_key_exists($value, $config)) { throw new Exception('Staple_ADAuthAdapter configuration error.',Staple_Error::AUTH_ERROR); } } if($config['adapter'] != get_class($this)) { throw new Exception('Staple_ADAuthAdapter configuration error.',Staple_Error::AUTH_ERROR); } return true; } /** * Returns the User ID from the adapter. * @return string * @see Staple_AuthAdapter::getUserId() */ public function getUserId() { return $this->uid; } } ?>