1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
<?php
use Carbon\Carbon as Carbon;
class Sec {
static $instance;
public function __construct($user_id, $formid, $access, $companyid = 1){ $this->user_id = $user_id; $this->formid = $formid; $this->access = $access; $this->companyid = $companyid;
$this->init(); }
public function init(){ $this->menuStr = val_in_array(self::getsecmenu($this->user_id, $this->companyid), 'str'); $this->contentStr = val_in_array(self::getseccontent($this->user_id, $formid, $this->companyid), 'str'); // vdump(__FUNCTION__); // vdump("MenuStr: {$this->menuStr}", "ContentStr: {$this->contentStr}"); }
public function getsecmenu(){ $sql = "SELECT sys_cms_login.cmsloginid, sys_cms_login.cmsloginname, GROUP_CONCAT(DISTINCT LEFT(formid,3)) as str FROM sys_cms_login INNER JOIN profile_user ON profile_user.user_id = sys_cms_login.cmsloginid INNER JOIN profile_formid ON profile_formid.profile_id = profile_user.profile_id WHERE LENGTH(sec_content)>0 AND cmsloginid = ?";
return bind_pdo($sql, array($this->user_id), "selectone"); }
public function getseccontent(){ $sql = "SELECT sys_cms_login.cmsloginid, sys_cms_login.cmsloginname, GROUP_CONCAT(DISTINCT sec_content) as str FROM sys_cms_login INNER JOIN profile_user ON profile_user.user_id = sys_cms_login.cmsloginid INNER JOIN profile_formid ON profile_formid.profile_id = profile_user.profile_id WHERE cmsloginid = ? AND formid = ?";
return bind_pdo($sql, array($this->user_id, $this->formid), "selectone"); }
//vdump($menu_permissionstr); public function haveMenuPermission($reqpermission, $menu_permissionstr=""){ //return true; if(empty($menu_permissionstr)){ $menu_permissionstr = $this->menuStr; } return strpos($menu_permissionstr, $reqpermission) !== false; }
//function havePermission($userid, $formid, $companyid){ public function havePermission($reqpermission, $permissionstr=""){//, $formid="", $companyid=""){ //return true; if(empty($permissionstr)){ $permissionstr = $this->contentStr; } // vdump( $permissionstr, $reqpermission ); //exit;
if( is_array($reqpermission) ){ foreach($reqpermission as $rp){ // vdump($rp); if(strpos($permissionstr, $rp) === false){ return false; } } return true; } return strpos($permissionstr, $reqpermission) !== false; }
public function check(){ if(!$this->formid){ // throw new Exception("Missing page_settings[formid]"); }
if($this->access){ $access = is_array($this->access) ? implode(',', $this->access) : $this->access; // vdump($this->access, $access); exit; if(!$this->havePermission($this->access)){ echo "<script>alert('Access Denied!'); location.href='logout.php'</script>"; //echo "<script>alert('Access Denied!'); location.href='index.php'</script>"; exit; //throw new Exception("Invalid permission on Form: {$this->formid} Access: {$access}"); } else{ // vdump("I have {$access} access"); } } }
static function getInstance(){ if(!self::$instance){ global $page_settings; return self::$instance = new self($_SESSION['cmsloginid'], $page_settings['formid'], $page_settings['access']); } return self::$instance; } }
if(!function_exists('grant')){ function grant($reqpermission){ return Sec::getInstance()->havePermission($reqpermission); } }
if(!function_exists('grantMenu')){ function grantMenu($reqpermission){ return Sec::getInstance()->haveMenuPermission($reqpermission); } }
// // call this before using grant() and grantMenu() // Sec::getInstance()->check();
// vdump(grant('GNr')); // vdump(grant('GNd')); // vdump(grantMenu('Pro')); // vdump(grantMenu('Prx'));
// // to check another form // $sec = new Sec(1, 'Product', ['GNr','GNu']); // vdump($sec, $sec->check());
|