480 lines
12 KiB
PHP
480 lines
12 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the symfony package.
|
|
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
/**
|
|
*
|
|
* @package symfony
|
|
* @subpackage plugin
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
* @version SVN: $Id: sfGuardUser.php 5064 2009-02-19 14:57:15Z bartek $
|
|
*/
|
|
class sfGuardUser extends BasesfGuardUser
|
|
{
|
|
protected
|
|
$profile = null,
|
|
$groups = null,
|
|
$permissions = null,
|
|
$allPermissions = null,
|
|
$modulePermissions = null;
|
|
|
|
/**
|
|
* Rabat ogólny
|
|
*
|
|
* @var DiscountUser
|
|
*/
|
|
protected $generalDiscount = null;
|
|
|
|
/**
|
|
* Domyślne dane bilingowe
|
|
*
|
|
* @var UserData
|
|
*/
|
|
protected $defaultUserBillingData = null;
|
|
|
|
/**
|
|
* Domyślne dane dostawy
|
|
*
|
|
* @var UserData
|
|
*/
|
|
protected $defaultUserDeliveryData = null;
|
|
|
|
|
|
public function getAdminGeneratorTitle()
|
|
{
|
|
return $this->getUsername();
|
|
}
|
|
|
|
|
|
public function __toString()
|
|
{
|
|
return $this->getUsername();
|
|
}
|
|
|
|
public function setPassword($password)
|
|
{
|
|
if (!$password && 0 == strlen($password))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!$salt = $this->getSalt())
|
|
{
|
|
$salt = md5(rand(100000, 999999) . $this->getUsername());
|
|
$this->setSalt($salt);
|
|
}
|
|
$algorithm = sfConfig::get('app_sf_guard_plugin_algorithm_callable', 'sha1');
|
|
$algorithmAsStr = is_array($algorithm) ? $algorithm[0] . '::' . $algorithm[1] : $algorithm;
|
|
if (!is_callable($algorithm))
|
|
{
|
|
throw new sfException(sprintf('The algorithm callable "%s" is not callable.', $algorithmAsStr));
|
|
}
|
|
$this->setAlgorithm($algorithmAsStr);
|
|
|
|
parent::setPassword(call_user_func_array($algorithm, array($salt . $password)));
|
|
}
|
|
|
|
public function setPasswordBis($password)
|
|
{
|
|
}
|
|
|
|
public function checkPassword($password)
|
|
{
|
|
if ($callable = sfConfig::get('app_sf_guard_plugin_check_password_callable'))
|
|
{
|
|
return call_user_func_array($callable, array($this->getUsername(), $password, $this));
|
|
}
|
|
else
|
|
{
|
|
return $this->checkPasswordByGuard($password);
|
|
}
|
|
}
|
|
|
|
public function checkPasswordByGuard($password)
|
|
{
|
|
$algorithm = $this->getAlgorithm();
|
|
if (false !== $pos = strpos($algorithm, '::'))
|
|
{
|
|
$algorithm = array(substr($algorithm, 0, $pos), substr($algorithm, $pos + 2));
|
|
}
|
|
if (!is_callable($algorithm))
|
|
{
|
|
throw new sfException(sprintf('The algorithm callable "%s" is not callable.', $algorithm));
|
|
}
|
|
|
|
return $this->getPassword() == call_user_func_array($algorithm, array($this->getSalt() . $password));
|
|
}
|
|
|
|
public function getProfile()
|
|
{
|
|
if (!is_null($this->profile))
|
|
{
|
|
return $this->profile;
|
|
}
|
|
|
|
$profileClass = sfConfig::get('app_sf_guard_plugin_profile_class', 'sfGuardUserProfile');
|
|
if (!class_exists($profileClass))
|
|
{
|
|
throw new sfException(sprintf('The user profile class "%s" does not exist.', $profileClass));
|
|
}
|
|
|
|
$fieldName = sfConfig::get('app_sf_guard_plugin_profile_field_name', 'user_id');
|
|
$profilePeerClass = $profileClass . 'Peer';
|
|
|
|
// to avoid php segmentation fault
|
|
class_exists($profilePeerClass);
|
|
|
|
$foreignKeyColumn = call_user_func_array(array($profilePeerClass, 'translateFieldName'), array($fieldName, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME));
|
|
|
|
if (!$foreignKeyColumn)
|
|
{
|
|
throw new sfException(sprintf('The user profile class "%s" does not contain a "%s" column.', $profileClass, $fieldName));
|
|
}
|
|
|
|
$c = new Criteria();
|
|
$c->add($foreignKeyColumn, $this->getId());
|
|
|
|
$this->profile = call_user_func_array(array($profileClass . 'Peer', 'doSelectOne'), array($c));
|
|
|
|
if (!$this->profile)
|
|
{
|
|
$this->profile = new $profileClass();
|
|
if (method_exists($this->profile, 'setsfGuardUser'))
|
|
{
|
|
$this->profile->setsfGuardUser($this);
|
|
}
|
|
else
|
|
{
|
|
$method = 'set' . call_user_func_array(array($profilePeerClass, 'translateFieldName'), array($fieldName, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME));
|
|
$this->profile->$method($this->getId());
|
|
}
|
|
}
|
|
|
|
return $this->profile;
|
|
}
|
|
|
|
public function addGroupByName($name, $con = null)
|
|
{
|
|
$group = sfGuardGroupPeer::retrieveByName($name);
|
|
if (!$group)
|
|
{
|
|
throw new Exception(sprintf('The group "%s" does not exist.', $name));
|
|
}
|
|
|
|
$ug = new sfGuardUserGroup();
|
|
$ug->setsfGuardUser($this);
|
|
$ug->setGroupId($group->getId());
|
|
|
|
$ug->save($con);
|
|
}
|
|
|
|
public function addPermissionByName($name, $con = null)
|
|
{
|
|
$permission = sfGuardPermissionPeer::retrieveByName($name);
|
|
if (!$permission)
|
|
{
|
|
throw new Exception(sprintf('The permission "%s" does not exist.', $name));
|
|
}
|
|
|
|
$up = new sfGuardUserPermission();
|
|
$up->setsfGuardUser($this);
|
|
$up->setPermissionId($permission->getId());
|
|
|
|
$up->save($con);
|
|
}
|
|
|
|
public function hasGroup($name)
|
|
{
|
|
if (!$this->groups)
|
|
{
|
|
$this->getGroups();
|
|
}
|
|
|
|
return isset($this->groups[$name]);
|
|
}
|
|
|
|
public function getGroups()
|
|
{
|
|
if (!$this->groups)
|
|
{
|
|
$this->groups = array();
|
|
|
|
$c = new Criteria();
|
|
$c->add(sfGuardUserGroupPeer::USER_ID, $this->getId());
|
|
$ugs = sfGuardUserGroupPeer::doSelectJoinsfGuardGroup($c);
|
|
|
|
foreach ($ugs as $ug)
|
|
{
|
|
$group = $ug->getsfGuardGroup();
|
|
$this->groups[$group->getName()] = $group;
|
|
}
|
|
}
|
|
|
|
return $this->groups;
|
|
}
|
|
|
|
public function getGroupNames()
|
|
{
|
|
return array_keys($this->getGroups());
|
|
}
|
|
|
|
/**
|
|
* Zwraca rabat ogólny
|
|
* @return DiscountUser|false
|
|
*/
|
|
public function getGeneralDiscount()
|
|
{
|
|
if (null === $this->generalDiscount)
|
|
{
|
|
$discounts = $this->getDiscountUsers();
|
|
|
|
$this->generalDiscount = $discounts ? $discounts[0] : false;
|
|
}
|
|
|
|
return $this->generalDiscount;
|
|
}
|
|
|
|
/**
|
|
* Ustawia wartość rabatu ogólnego
|
|
*
|
|
* @param float $v
|
|
* @return void
|
|
*/
|
|
public function setGeneralDiscountValue($v)
|
|
{
|
|
$discount = $this->getGeneralDiscount();
|
|
|
|
if (!$discount)
|
|
{
|
|
$discount = new DiscountUser();
|
|
$this->addDiscountUser($discount);
|
|
$this->generalDiscount = $discount;
|
|
}
|
|
|
|
$discount->setDiscount($v);
|
|
}
|
|
|
|
/**
|
|
* Zwraca wartość rabatu ogólnego
|
|
*
|
|
* @return float
|
|
*/
|
|
public function getGeneralDiscountValue()
|
|
{
|
|
return $this->getGeneralDiscount() ? $this->getGeneralDiscount()->getDiscount() : 0;
|
|
}
|
|
|
|
/**
|
|
* Zwraca domyślne dane bilingowe
|
|
*
|
|
* @return UserData|false
|
|
*/
|
|
public function getDefaultUserBillingData()
|
|
{
|
|
if (null === $this->defaultUserBillingData)
|
|
{
|
|
$c = new Criteria();
|
|
$c->add(UserDataPeer::IS_BILLING, true);
|
|
$c->add(UserDataPeer::IS_DEFAULT, true);
|
|
$c->setLimit(1);
|
|
$data = $this->getUserDatas($c);
|
|
|
|
$this->defaultUserBillingData = $data ? $data[0] : false;
|
|
}
|
|
|
|
return $this->defaultUserBillingData;
|
|
}
|
|
|
|
/**
|
|
* Zwraca domyślne dane dostawy
|
|
*
|
|
* @return UserData|false
|
|
*/
|
|
public function getDefaultUserDeliveryData()
|
|
{
|
|
if (null === $this->defaultUserDeliveryData)
|
|
{
|
|
$c = new Criteria();
|
|
$c->add(UserDataPeer::IS_BILLING, false);
|
|
$c->add(UserDataPeer::IS_DEFAULT, true);
|
|
$c->setLimit(1);
|
|
$data = $this->getUserDatas($c);
|
|
|
|
$this->defaultUserDeliveryData = $data ? $data[0] : false;
|
|
}
|
|
|
|
return $this->defaultUserDeliveryData;
|
|
}
|
|
|
|
public function hasPermission($name)
|
|
{
|
|
if (!$this->permissions)
|
|
{
|
|
$this->getPermissions();
|
|
}
|
|
|
|
return isset($this->permissions[$name]);
|
|
}
|
|
|
|
public function getPermissions()
|
|
{
|
|
if (!$this->permissions)
|
|
{
|
|
$this->permissions = array();
|
|
|
|
$c = new Criteria();
|
|
$c->add(sfGuardUserPermissionPeer::USER_ID, $this->getId());
|
|
$ups = sfGuardUserPermissionPeer::doSelectJoinsfGuardPermission($c);
|
|
|
|
foreach ($ups as $up)
|
|
{
|
|
$permission = $up->getsfGuardPermission();
|
|
$this->permissions[$permission->getName()] = $permission;
|
|
}
|
|
}
|
|
|
|
return $this->permissions;
|
|
}
|
|
|
|
public function getModulePermissionNames()
|
|
{
|
|
if (null === $this->modulePermissions)
|
|
{
|
|
$permission = sfGuardUserModulePermissionPeer::retrieveByPK($this->getId());
|
|
|
|
$permissions = $permission ? $permission->getPermissions() : array();
|
|
|
|
if (!$permissions)
|
|
{
|
|
$permissions = array();
|
|
}
|
|
|
|
foreach (sfGuardUserGroupPeer::doSelectModulePermissionsByUser($this) as $permission)
|
|
{
|
|
$permissions = array_merge($permissions, $permission->getPermissions());
|
|
}
|
|
|
|
$permissions[] = 'sfGuardUser.modification';
|
|
|
|
$this->modulePermissions = $permissions;
|
|
}
|
|
|
|
|
|
return $this->modulePermissions;
|
|
}
|
|
|
|
public function hasModulePermission($name)
|
|
{
|
|
if (!$this->modulePermissions)
|
|
{
|
|
$this->getModulePermissionNames();
|
|
}
|
|
|
|
return in_array($name, $this->modulePermissions);
|
|
}
|
|
|
|
public function getPermissionNames()
|
|
{
|
|
return array_keys($this->getPermissions());
|
|
}
|
|
|
|
// merge of permission in a group + permissions
|
|
public function getAllPermissions()
|
|
{
|
|
if (!$this->allPermissions)
|
|
{
|
|
$this->allPermissions = $this->getPermissions();
|
|
|
|
foreach (sfGuardUserGroupPeer::doSelectPermissionsByUser($this) as $permission)
|
|
{
|
|
$this->allPermissions[$permission->getName()] = $permission;
|
|
}
|
|
}
|
|
|
|
return $this->allPermissions;
|
|
}
|
|
|
|
public function getAllPermissionNames()
|
|
{
|
|
return array_keys($this->getAllPermissions());
|
|
}
|
|
|
|
public function reloadGroupsAndPermissions()
|
|
{
|
|
$this->groups = null;
|
|
$this->permissions = null;
|
|
$this->allPermissions = null;
|
|
}
|
|
|
|
public function delete($con = null)
|
|
{
|
|
// delete profile if available
|
|
try
|
|
{
|
|
if ($profile = $this->getProfile())
|
|
{
|
|
$profile->delete();
|
|
}
|
|
}
|
|
catch (sfException $e)
|
|
{
|
|
}
|
|
|
|
$ret = parent::delete();
|
|
|
|
self::clearCache();
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public function setPasswordHash($v)
|
|
{
|
|
if (!is_null($v) && !is_string($v))
|
|
{
|
|
$v = (string)$v;
|
|
}
|
|
|
|
if ($this->password !== $v)
|
|
{
|
|
$this->password = $v;
|
|
$this->modifiedColumns[] = sfGuardUserPeer::PASSWORD;
|
|
}
|
|
}
|
|
|
|
public function save($con = null)
|
|
{
|
|
if ($this->isNew() && !$this->isColumnModified(sfGuardUserPeer::IS_CONFIRM))
|
|
{
|
|
$this->setIsConfirm(false);
|
|
}
|
|
|
|
if ($this->getHashCode() == "")
|
|
{
|
|
$this->setHashCode(md5(microtime()));
|
|
}
|
|
|
|
if ($this->isColumnModified(sfGuardUserPeer::PASSWORD))
|
|
{
|
|
$this->setLastPasswordChange(time());
|
|
}
|
|
|
|
$ret = parent::save($con);
|
|
|
|
self::clearCache();
|
|
|
|
return $ret;
|
|
}
|
|
|
|
public static function clearCache()
|
|
{
|
|
$fc = stFunctionCache::getInstance('sfGuardUser');
|
|
$fc->removeAll();
|
|
}
|
|
}
|