PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Quản lý website bán mỹ phẩm PHP/administrator/components/com_virtuemart/helpers/permissions.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 511 lines | 287 code | 77 blank | 147 comment | 75 complexity | 45d31d63bf871c91df707207fab39c8f MD5 | raw file
  1. <?php
  2. if( !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. *
  5. * @version $Id: permissions.php 6053 2012-06-05 12:36:21Z Milbo $
  6. * @package VirtueMart
  7. * @subpackage classes
  8. * @author Sören
  9. * @author Max Milbers
  10. * @copyright Copyright (C) 2010-2011 Virtuemart Team - All rights reserved.
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  12. * VirtueMart is free software. This version may have been modified pursuant
  13. * to the GNU General Public License, and as distributed it includes or
  14. * is derivative of works licensed under the GNU General Public License or
  15. * other free or open source software licenses.
  16. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  17. *
  18. * http://virtuemart.org
  19. */
  20. /**
  21. * The permission handler class for VirtueMart.
  22. *
  23. * @todo Further cleanup
  24. */
  25. class Permissions extends JObject{
  26. /** @var array Contains all the user groups */
  27. var $_user_groups;
  28. /** @var virtuemart_user_id for the permissions*/
  29. var $_virtuemart_user_id; //$auth['virtuemart_user_id']
  30. var $_show_prices; //$auth['show_prices']
  31. var $_db;
  32. private $_perms;
  33. var $_is_registered_customer;
  34. private $_vendorId = false;
  35. static $_instance;
  36. public function __construct() {
  37. $this->_db = JFactory::getDBO();
  38. $this->getUserGroups();
  39. $this->doAuthentication();
  40. }
  41. static public function getInstance() {
  42. if(!is_object(self::$_instance)){
  43. self::$_instance = new Permissions();
  44. }else {
  45. }
  46. return self::$_instance;
  47. }
  48. public function getUserGroups() {
  49. if (empty($this->_user_groups)) {
  50. $this->_db = JFactory::getDBO();
  51. $q = ('SELECT `virtuemart_permgroup_id`,`group_name`,`group_level`
  52. FROM `#__virtuemart_permgroups`
  53. ORDER BY `group_level` ');
  54. $this->_db->setQuery($q);
  55. $this->_user_groups = $this->_db->loadObjectList('group_name');
  56. }
  57. // echo 'Die Usergroups: <pre>'.print_r($this->_user_groups).'</pre>';
  58. return $this->_user_groups;
  59. }
  60. /**
  61. * Get permissions for a user ID
  62. *
  63. * @param int $virtuemart_user_id the user ID to check. If no user ID is given the currently logged in user will be used.
  64. * @return string permissions
  65. */
  66. public function getPermissions ($userId=null) {
  67. // default to current user
  68. if ($userId == null) {
  69. $user = JFactory::getUser();
  70. $userId = $user->id;
  71. }
  72. // only re-run authentication if we have a different user
  73. if ($userId != $this->_virtuemart_user_id) {
  74. $this->doAuthentication($userId);
  75. }
  76. return $this->_perms;
  77. }
  78. /**
  79. * description: Validates if someone is registered customer.
  80. * by checking if one has a billing address
  81. * parameters: virtuemart_user_id
  82. * returns: true if the user has a BT address
  83. * false if the user has none
  84. *
  85. * Check if a user is registered in the shop (=customer)
  86. *
  87. * @param int $virtuemart_user_id the user ID to check. If no user ID is given the currently logged in user will be used.
  88. * @return boolean
  89. */
  90. public function isRegisteredCustomer($virtuemart_user_id=0) {
  91. if ($virtuemart_user_id == 0) {
  92. /* Lets see if we can get the current signed in user */
  93. $user = JFactory::getUser();
  94. if ($user->id == 0) return false;
  95. else $virtuemart_user_id = $user->id;
  96. }
  97. $this->_db = JFactory::getDBO();
  98. /* If the registration type is neither "no registration" nor "optional registration",
  99. there *must* be a related Joomla! user, we can join */
  100. if (VmConfig::get('vm_registration_type') != 'NO_REGISTRATION'
  101. && VmConfig::get('vm_registration_type') != 'OPTIONAL_REGISTRATION') {
  102. $q = "SELECT COUNT(virtuemart_user_id) AS num_rows
  103. FROM `#__virtuemart_userinfos`, `#__users`
  104. WHERE `id`=`virtuemart_user_id`
  105. AND #__virtuemart_userinfos.virtuemart_user_id='" . (int)$virtuemart_user_id . "'
  106. AND #__virtuemart_userinfos.address_type='BT'";
  107. }
  108. else {
  109. $q = "SELECT COUNT(virtuemart_user_id) AS num_rows
  110. FROM `#__virtuemart_userinfos`
  111. WHERE #__virtuemart_userinfos.virtuemart_user_id='" . (int)$virtuemart_user_id . "'
  112. AND #__virtuemart_userinfos.address_type='BT'";
  113. }
  114. $this->_db->setQuery($q);
  115. return $this->_db->loadResult();
  116. }
  117. /**
  118. * This function does the basic authentication
  119. * for a user in the shop.
  120. * It assigns permissions, the name, country, zip and
  121. * the shopper group id with the user and the session.
  122. * @return array Authentication information
  123. */
  124. function doAuthentication ($user_id=null) {
  125. $this->_db = JFactory::getDBO();
  126. $session = JFactory::getSession();
  127. $user = JFactory::getUser($user_id);
  128. // Check token
  129. //JRequest::checkToken() or jexit( 'Invalid Token doAuthentication' );
  130. if (VmConfig::get('vm_price_access_level') != '') {
  131. // Is the user allowed to see the prices?
  132. $this->_show_prices = $user->authorize( 'virtuemart', 'prices' );
  133. }
  134. else {
  135. $this->_show_prices = 1;
  136. }
  137. if(!empty($user->id)){
  138. $this->_virtuemart_user_id = $user->id;
  139. $q = 'SELECT `perms` FROM #__virtuemart_vmusers
  140. WHERE virtuemart_user_id="'.(int)$this->_virtuemart_user_id.'"';
  141. $this->_db->setQuery($q);
  142. $this->_perms = $this->_db->loadResult();
  143. //We must prevent that Administrators or Managers are 'just' shoppers
  144. //TODO rewrite it working correctly with jooomla ACL
  145. if(JVM_VERSION === 2 ){
  146. if($user->authorise('core.admin')){
  147. $this->_perms = 'admin';
  148. }
  149. } else {
  150. if(strpos($user->usertype,'Administrator')!== false){
  151. $this->_perms = "admin";
  152. }
  153. }
  154. if(empty($this->_perms)){
  155. if(JVM_VERSION === 2 ){
  156. if($user->groups){
  157. if($user->authorise('core.admin')){
  158. $this->_perms = 'admin';
  159. } else if($user->authorise('core.manage')){
  160. $this->_perms = 'storeadmin';
  161. } else {
  162. $this->_perms = 'shopper';
  163. }
  164. } else {
  165. $this->_perms = 'shopper';
  166. }
  167. } else {
  168. if(strpos($user->usertype,'Administrator')!== false){
  169. $this->_perms = "admin";
  170. } else if(strpos($user->usertype,'Manager')!== false){
  171. $this->_perms = "storeadmin";
  172. } else {
  173. $this->_perms = "shopper";
  174. }
  175. }
  176. }
  177. // vmdebug('$user->authorise perms '.$this->_perms);
  178. //}
  179. $this->_is_registered_customer = true;
  180. } else {
  181. $this->_virtuemart_user_id = 0;
  182. $this->_perms = "shopper";
  183. $this->_is_registered_customer = false;
  184. }
  185. }
  186. /**
  187. * Validates the permission to do something.
  188. *
  189. * @param string $perms
  190. * @return boolean Check successful or not
  191. * @example $perm->check( 'admin,storeadmin' );
  192. * returns true when the user is admin or storeadmin
  193. */
  194. public function check($perms,$acl=0) {
  195. /* Set the authorization for use */
  196. // Parse all permissions in argument, comma separated
  197. // It is assumed auth_user only has one group per user.
  198. $p1 = explode(",", $this->_perms);
  199. $p2 = explode(",", $perms);
  200. // vmdebug('check '.$perms,$p1,$p2);
  201. while (list($key1, $value1) = each($p1)) {
  202. while (list($key2, $value2) = each($p2)) {
  203. if ($value1 == $value2) {
  204. return true;
  205. }
  206. }
  207. }
  208. // vmdebug('return false for ',$perms,$this->_perms);
  209. return false;
  210. }
  211. /**
  212. * Checks if user is admin or has vendorId=1,
  213. * if superadmin, but not a vendor it gives back vendorId=1 (single vendor, but multiuser administrated)
  214. *
  215. * @author Mattheo Vicini
  216. * @author Max Milbers
  217. */
  218. public function isSuperVendor(){
  219. if(!$this->_vendorId){
  220. $user = JFactory::getUser();
  221. if(!empty( $user->id)){
  222. $q = 'SELECT `virtuemart_vendor_id`
  223. FROM `#__virtuemart_vmusers` `au`
  224. LEFT JOIN `#__virtuemart_userinfos` `u`
  225. ON (au.virtuemart_user_id = u.virtuemart_user_id)
  226. WHERE `u`.`virtuemart_user_id`="' .$user->id.'" AND `au`.`user_is_vendor` = "1" ';
  227. $db= JFactory::getDbo();
  228. $db->setQuery($q);
  229. $virtuemart_vendor_id = $db->loadResult();
  230. if ($virtuemart_vendor_id) {
  231. $this->_vendorId = $virtuemart_vendor_id;
  232. } else {
  233. $this->_vendorId = 0;
  234. }
  235. } else {
  236. return false;
  237. }
  238. }
  239. if($this->_vendorId!=0){
  240. return $this->_vendorId;
  241. } else {
  242. if($this->check('admin,storeadmin') ){
  243. $this->_vendorId = 1;
  244. return $this->_vendorId;
  245. } else {
  246. return false;
  247. }
  248. }
  249. return false;
  250. }
  251. /**
  252. * Checks if the user has higher permissions than $perm
  253. * does not work properly, do not use or correct it
  254. * @param string $perm
  255. * @return boolean
  256. * @example $perm->hasHigherPerms( 'storeadmin' );
  257. * returns true when user is admin
  258. */
  259. function atLeastPerms( $perm ) {
  260. if( $this->_perms && $this->_user_groups[$perm] >= $this->_user_groups[$this->_perms] ) {
  261. return true;
  262. }
  263. else {
  264. return false;
  265. }
  266. }
  267. /**
  268. * lists the permission levels in a select box
  269. * @author pablo
  270. * @param string $name The name of the select element
  271. * @param string $group_name The preselected key
  272. */
  273. function list_perms( $name, $group_name, $size=1, $multi=false ) {
  274. $auth = $_SESSION['auth'];
  275. if( $multi ) {
  276. $multi = 'multiple="multiple"';
  277. }
  278. // Get users current permission value
  279. $dvalue = $this->user_groups[$this->_perms];
  280. $perms = $this->getUserGroups();
  281. arsort( $perms );
  282. if( $size==1 ) {
  283. $values[0] = JText::_('COM_VIRTUEMART_SELECT');
  284. }
  285. foreach($perms as $key => $value) {
  286. // Display only those permission that this user can set
  287. if ($value >= $dvalue) {
  288. $values[$key] = $key;
  289. }
  290. }
  291. if( $size > 1 ) {
  292. $name .= '[]';
  293. $values['none'] = JText::_('COM_VIRTUEMART_NO_RESTRICTION');
  294. }
  295. echo VmHTML::selectList( $name, $group_name, $values, $size, $multi );
  296. }
  297. /**
  298. * Here we insert groups that are allowed to view prices
  299. *
  300. */
  301. function prepareACL() {
  302. // The basic ACL integration in Mambo/Joomla is not awesome
  303. $child_groups = self::getChildGroups( '#__core_acl_aro_groups', 'g1.virtuemart_shoppergroup_id, g1.name, COUNT(g2.name) AS level', 'g1.name', null, VmConfig::get('vm_price_access_level'));
  304. echo '<pre>'.print_r($child_groups,1).'</pre>';
  305. foreach( $child_groups as $child_group ) {
  306. self::_addToGlobalACL( 'virtuemart', 'prices', 'users', $child_group->name, null, null );
  307. }
  308. $admin_groups = self::getChildGroups( '#__core_acl_aro_groups', 'g1.virtuemart_shoppergroup_id, g1.name, COUNT(g2.name) AS level', 'g1.name', null, 'Public Backend' );
  309. foreach( $admin_groups as $child_group ) {
  310. self::_addToGlobalACL( 'virtuemart', 'prices', 'users', $child_group->name, null, null );
  311. }
  312. }
  313. /**
  314. * Function from an old Mambo phpgacl integration function
  315. * @deprecated (but necessary, sigh!)
  316. * @static
  317. * @param string $table
  318. * @param string $fields
  319. * @param string $groupby
  320. * @param int $root_id
  321. * @param string $root_name
  322. * @param boolean $inclusive
  323. * @return array
  324. */
  325. function getChildGroups($table, $fields, $groupby=null, $root_id=null, $root_name=null, $inclusive=true) {
  326. $database = JFactory::getDBO();
  327. $root = new stdClass();
  328. $root->lft = 0;
  329. $root->rgt = 0;
  330. $fields = str_replace( 'virtuemart_shoppergroup_id', 'id', $fields );
  331. if ($root_id) {
  332. }
  333. else if ($root_name) {
  334. $database->setQuery("SELECT `lft`, `rgt` FROM `".$table."` WHERE `name`='".$root_name."'" );
  335. $root = $database->loadObject();
  336. }
  337. $where = '';
  338. if ($root->lft+$root->rgt != 0) {
  339. if ($inclusive) {
  340. $where = "WHERE g1.lft BETWEEN $root->lft AND $root->rgt";
  341. } else {
  342. $where = "WHERE g1.lft BETWEEN $root->lft+1 AND $root->rgt-1";
  343. }
  344. }
  345. $database->setQuery( "SELECT ".$fields
  346. . "\nFROM ".$table." AS g1"
  347. . "\nINNER JOIN ".$table." AS g2 ON g1.lft BETWEEN g2.lft AND g2.rgt"
  348. . "\n". $where
  349. . ($groupby ? "\nGROUP BY ".$groupby : "")
  350. . "\nORDER BY g1.lft"
  351. );
  352. return $database->loadObjectList();
  353. }
  354. /**
  355. * This is a temporary function to allow 3PD's to add basic ACL checks for their
  356. * modules and components. NOTE: this information will be compiled in the db
  357. * in future versions
  358. * @static
  359. * @param unknown_type $aco_section_value
  360. * @param unknown_type $aco_value
  361. * @param unknown_type $aro_section_value
  362. * @param unknown_type $aro_value
  363. * @param unknown_type $axo_section_value
  364. * @param unknown_type $axo_value
  365. */
  366. function _addToGlobalACL( $aco_section_value, $aco_value, $aro_section_value, $aro_value, $axo_section_value=NULL, $axo_value=NULL ) {
  367. global $acl;
  368. $acl->acl[] = array( $aco_section_value, $aco_value, $aro_section_value, $aro_value, $axo_section_value, $axo_value );
  369. $acl->acl_count = count( $acl->acl );
  370. }
  371. /**
  372. * Returns a tree with the children of the root group id
  373. * @static
  374. * @param int $root_id
  375. * @param string $root_name
  376. * @param boolean $inclusive
  377. * @return unknown
  378. */
  379. function getGroupChildrenTree( $root_id=null, $root_name=null, $inclusive=true ) {
  380. global $database, $_VERSION;
  381. $tree = ps_perm::getChildGroups( '#__core_acl_aro_groups',
  382. 'g1.virtuemart_shoppergroup_id, g1.name, COUNT(g2.name) AS level',
  383. 'g1.name',
  384. $root_id, $root_name, $inclusive );
  385. // first pass get level limits
  386. $n = count( $tree );
  387. $min = $tree[0]->level;
  388. $max = $tree[0]->level;
  389. for ($i=0; $i < $n; $i++) {
  390. $min = min( $min, $tree[$i]->level );
  391. $max = max( $max, $tree[$i]->level );
  392. }
  393. $indents = array();
  394. foreach (range( $min, $max ) as $i) {
  395. $indents[$i] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  396. }
  397. // correction for first indent
  398. $indents[$min] = '';
  399. $list = array();
  400. for ($i=$n-1; $i >= 0; $i--) {
  401. $shim = '';
  402. foreach (range( $min, $tree[$i]->level ) as $j) {
  403. $shim .= $indents[$j];
  404. }
  405. if (@$indents[$tree[$i]->level+1] == '.&nbsp;') {
  406. $twist = '&nbsp;';
  407. } else {
  408. $twist = "-&nbsp;";
  409. }
  410. if( $_VERSION->PRODUCT == 'Joomla!' && $_VERSION->RELEASE >= 1.5 ) {
  411. $tree[$i]->virtuemart_shoppergroup_id = $tree[$i]->id;
  412. }
  413. $list[$tree[$i]->virtuemart_shoppergroup_id] = $shim.$twist.$tree[$i]->name;
  414. if ($tree[$i]->level < @$tree[$i-1]->level) {
  415. $indents[$tree[$i]->level+1] = '.&nbsp;';
  416. }
  417. }
  418. ksort($list);
  419. return $list;
  420. }
  421. /**
  422. * Check if the price should be shown including tax
  423. *
  424. * @author RolandD
  425. * @todo Figure out where to get the setting from
  426. * @access public
  427. * @param
  428. * @return bool true if price with tax is shown otherwise false
  429. */
  430. public function showPriceIncludingTax() {
  431. return true;
  432. }
  433. }
  434. //pure php no closing tag