[*] BO : Rights verification for multi-shop

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9198 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
lLefevre
2011-10-10 16:10:57 +00:00
parent 5fe22693dd
commit a8a11c0ece
8 changed files with 155 additions and 51 deletions
+14 -3
View File
@@ -222,6 +222,7 @@ class AdminControllerCore extends Controller
{
if ($disable)
return true;
$this->tabAccess = Profile::getProfileAccess($this->context->employee->id_profile, $this->id);
if ($this->tabAccess['view'] === '1')
@@ -1050,14 +1051,24 @@ class AdminControllerCore extends Controller
Tools::redirectAdmin($url['path'].'?'.http_build_query($parseQuery));
}
$shopID = '';
$shop_id = '';
if ($this->context->cookie->shopContext)
{
$split = explode('-', $this->context->cookie->shopContext);
if (count($split) == 2 && $split[0] == 's')
$shopID = (int)$split[1];
$shop_id = (int)$split[1];
}
$this->context->shop = new Shop($shopID);
else if ($this->context->employee->id_profile == _PS_ADMIN_PROFILE_)
$shop_id = '';
else if ($this->context->shop->getTotalShopsWhoExists() != Employee::getTotalEmployeeShopById((int)$this->context->employee->id))
{
$shops = Employee::getEmployeeShopById((int)$this->context->employee->id);
$shop_id = (int)$shops[0];
}
else
Employee::getEmployeeShopAccess((int)$this->context->employee->id);
$this->context->shop = new Shop($shop_id);
/* Filter memorization */
if (isset($_POST) && !empty($_POST) && isset($this->table))
+3 -2
View File
@@ -898,7 +898,8 @@ abstract class AdminTabCore
if (!Shop::isMultiShopActivated())
return ;
$assos = self::getAssoShop($this->table, $id_object);
if(!$assos = self::getAssoShop($this->table, $id_object))
return;
Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.$this->table.'_'.$assos[1].($id_object ? ' WHERE `'.$this->identifier.'`='.(int)$id_object : ''));
foreach ($assos[0] as $asso)
@@ -915,7 +916,7 @@ abstract class AdminTabCore
else if (isset($groupShopAsso[$table]) && $groupShopAsso[$table]['type'] == 'group_shop')
$type = 'group_shop';
else
return ;
return;
$assos = array();
foreach ($_POST as $k => $row)
+84 -28
View File
@@ -27,60 +27,60 @@
class EmployeeCore extends ObjectModel
{
public $id;
public $id;
/** @var string Determine employee profile */
public $id_profile;
public $id_profile;
/** @var string employee language */
public $id_lang;
public $id_lang;
/** @var string Lastname */
public $lastname;
public $lastname;
/** @var string Firstname */
public $firstname;
public $firstname;
/** @var string e-mail */
public $email;
public $email;
/** @var string Password */
public $passwd;
public $passwd;
/** @var datetime Password */
public $last_passwd_gen;
public $last_passwd_gen;
public $stats_date_from;
public $stats_date_to;
/** @var string Display back office background in the specified color */
public $bo_color;
public $bo_color;
/** @var string employee's chosen theme */
public $bo_theme;
public $bo_theme;
/** @var string / enum hover or click mode */
public $bo_uimode;
public $bo_uimode;
/** @var bool, true */
public $bo_show_screencast;
public $bo_show_screencast;
/** @var boolean Status */
public $active = 1;
public $active = 1;
/** @var boolean show screencast */
public $show_screencast = 1;
public $show_screencast = 1;
public $remote_addr;
public $remote_addr;
protected $fieldsRequired = array('lastname', 'firstname', 'email', 'passwd', 'id_profile', 'id_lang');
protected $fieldsSize = array('lastname' => 32, 'firstname' => 32, 'email' => 128, 'passwd' => 32, 'bo_color' => 32, 'bo_theme' => 32);
protected $fieldsValidate = array('lastname' => 'isName', 'firstname' => 'isName', 'email' => 'isEmail', 'id_lang' => 'isUnsignedInt',
protected $fieldsRequired = array('lastname', 'firstname', 'email', 'passwd', 'id_profile', 'id_lang');
protected $fieldsSize = array('lastname' => 32, 'firstname' => 32, 'email' => 128, 'passwd' => 32, 'bo_color' => 32, 'bo_theme' => 32);
protected $fieldsValidate = array('lastname' => 'isName', 'firstname' => 'isName', 'email' => 'isEmail', 'id_lang' => 'isUnsignedInt',
'passwd' => 'isPasswdAdmin', 'active' => 'isBool', 'id_profile' => 'isInt', 'bo_color' => 'isColor', 'bo_theme' => 'isGenericName',
'bo_uimode' => 'isGenericName', 'show_screencast' => 'isBool', 'bo_show_screencast' => 'isBool');
protected $table = 'employee';
protected $identifier = 'id_employee';
protected $table = 'employee';
protected $identifier = 'id_employee';
protected $webserviceParameters = array(
'fields' => array(
@@ -123,10 +123,10 @@ class EmployeeCore extends ObjectModel
return $fields;
}
public function add($autodate = true, $nullValues = true)
public function add($autodate = true, $null_values = true)
{
$this->last_passwd_gen = date('Y-m-d H:i:s', strtotime('-'.Configuration::get('PS_PASSWD_TIME_BACK').'minutes'));
return parent::add($autodate, $nullValues);
return parent::add($autodate, $null_values);
}
/**
@@ -136,9 +136,9 @@ class EmployeeCore extends ObjectModel
* @param string $passwd Password is also checked if specified
* @return Employee instance
*/
public function getByEmail($email, $passwd = NULL)
public function getByEmail($email, $passwd = null)
{
if (!Validate::isEmail($email) OR ($passwd != NULL AND !Validate::isPasswd($passwd)))
if (!Validate::isEmail($email) || ($passwd != null && !Validate::isPasswd($passwd)))
die(Tools::displayError());
$result = Db::getInstance()->getRow('
@@ -151,7 +151,7 @@ class EmployeeCore extends ObjectModel
return false;
$this->id = $result['id_employee'];
$this->id_profile = $result['id_profile'];
foreach ($result AS $key => $value)
foreach ($result as $key => $value)
if (key_exists($key, $this))
$this->{$key} = $value;
return $this;
@@ -176,7 +176,7 @@ class EmployeeCore extends ObjectModel
*/
public static function checkPassword($id_employee, $passwd)
{
if (!Validate::isUnsignedId($id_employee) OR !Validate::isPasswd($passwd, 8))
if (!Validate::isUnsignedId($id_employee) || !Validate::isPasswd($passwd, 8))
die (Tools::displayError());
return Db::getInstance()->getValue('
@@ -187,13 +187,13 @@ class EmployeeCore extends ObjectModel
AND active = 1');
}
public static function countProfile($id_profile, $activeOnly = false)
public static function countProfile($id_profile, $active_only = false)
{
return Db::getInstance()->getValue('
SELECT COUNT(*)
FROM `'._DB_PREFIX_.'employee`
WHERE `id_profile` = '.(int)$id_profile.'
'.($activeOnly ? ' AND `active` = 1' : ''));
'.($active_only ? ' AND `active` = 1' : ''));
}
public function isLastAdmin()
@@ -240,4 +240,60 @@ class EmployeeCore extends ObjectModel
Context::getContext()->cookie->logout();
$this->id = null;
}
public static function getEmployeeShopAccess($id_employee)
{
$context = Context::getContext();
switch ($type = $context->shop->getContextType())
{
case 1:
info('context shop');
if ($context->shop->checkIfShopExist($context->shop->id))
{
if (!in_array($context->shop->id, self::getEmployeeShopById($id_employee)))
return false;
}
else
return false;
break;
case 2:
info('context group');
if ($context->shop->checkIfGroupShopExist($context->shop->getGroupID()))
{
$shops = $context->shop->getIdShopsByIdGroupShop($context->shop->getGroupID());
foreach ($shops as $shop)
if (!in_array($shop, self::getEmployeeShopById($id_employee)))
return false;
}
else
return false;
break;
case 3:
info('context all');
if ($context->employee->id_profile == _PS_ADMIN_PROFILE_ ||
$context->shop->getTotalShopsWhoExists() == self::getTotalEmployeeShopById($id_employee))
return true;
else
return false;
break;
}
return true;
}
public static function getTotalEmployeeShopById($id)
{
return (int)Db::getInstance()->getValue(sprintf('SELECT COUNT(*) FROM`'._DB_PREFIX_.'employee_shop` WHERE `id_employee` = %d', (int)$id));
}
public static function getEmployeeShopById($id)
{
$result = Db::getInstance()->executeS(sprintf('SELECT * FROM`'._DB_PREFIX_.'employee_shop` WHERE `id_employee` = %d', (int)$id));
$data = array();
foreach ($result as $group_data)
$data[] = (int)$group_data['id_shop'];
return $data;
}
}
+1 -1
View File
@@ -64,7 +64,7 @@ class ProfileCore extends ObjectModel
SELECT p.`id_profile`, `name`
FROM `'._DB_PREFIX_.'profile` p
LEFT JOIN `'._DB_PREFIX_.'profile_lang` pl ON (p.`id_profile` = pl.`id_profile` AND `id_lang` = '.(int)($id_lang).')
ORDER BY `name` ASC');
ORDER BY `id_profile` ASC');
}
/**
+29 -1
View File
@@ -440,6 +440,7 @@ class ShopCore extends ObjectModel
public static function getShops($active = true, $id_group_shop = null, $get_as_list_id = false)
{
Shop::cacheShops();
$results = array();
foreach (self::$shops as $group_id => $group_data)
foreach ($group_data['shops'] as $id => $shop_data)
@@ -492,12 +493,29 @@ class ShopCore extends ObjectModel
return count(Shop::getShops($active));
}
/**
* @return int Total of shops
*/
public static function getTotalShopsWhoExists()
{
return (int)Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'shop`');
}
/**
* @return int Total of shops
*/
public static function getTotalShopsByIdGroupShop($id)
{
return (int)Db::getInstance()->getValue(sprintf('SELECT COUNT(*) FROM '._DB_PREFIX_.'shop WHERE id_group_shop = %d', (int)$id));
return (int)Db::getInstance()->getValue(sprintf('SELECT COUNT(*) FROM `'._DB_PREFIX_.'shop` WHERE `id_group_shop` = %d', (int)$id));
}
public static function getIdShopsByIdGroupShop($id)
{
$result = Db::getInstance()->executeS(sprintf('SELECT `id_shop`, `id_group_shop` FROM `'._DB_PREFIX_.'shop` WHERE `id_group_shop` = %d', (int)$id));
$data = array();
foreach ($result as $group_data)
$data[] = (int)$group_data['id_shop'];
return $data;
}
/**
@@ -762,4 +780,14 @@ class ShopCore extends ObjectModel
}*/
}
}
public function checkIfShopExist($id)
{
return (int)Db::getInstance()->getValue(sprintf('SELECT COUNT(*) FROM`'._DB_PREFIX_.'shop` WHERE `id_shop` = %d', (int)$id));
}
public function checkIfGroupShopExist($id)
{
return (int)Db::getInstance()->getValue(sprintf('SELECT COUNT(*) FROM`'._DB_PREFIX_.'group_shop` WHERE `id_group_shop` = %d', (int)$id));
}
}