XOOPS  2.6.0
Permission.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  */
11 
12 namespace Xmf\Module;
13 
14 use Xmf\Loader;
17 
32 {
36  private $mid;
37 
41  private $dirname;
42 
46  private $db;
47 
51  private $perm;
52 
56  private $xoops = null;
57 
63  public function init()
64  {
65  if (!class_exists('XoopsGroupPermHandler', true)) {
66  Loader::loadFile(\XoopsBaseConfig::get('root-path') . '/kernel/groupperm.php');
67  }
68  $this->mid = $this->module->getVar('mid');
69  $this->dirname = $this->module->getVar('dirname');
71  $this->perm = new \XoopsGroupPermHandler($this->db);
72  if (class_exists('Xoops', false)) {
73  $this->xoops = \Xoops::getInstance();
74  }
75  }
76 
85  public function checkPermission($gperm_name, $gperm_itemid)
86  {
87  $gperm_groupid = $this->xoops->getUserGroups();
88 
89  return $this->perm->checkRight(
90  $gperm_name,
91  $gperm_itemid,
92  $gperm_groupid,
93  $this->mid
94  );
95  }
96 
108  public function checkPermissionRedirect(
109  $gperm_name,
110  $gperm_itemid,
111  $url,
112  $time = 3,
113  $message = ''
114  ) {
115  $gperm_groupid = $this->xoops->getUserGroups();
116  $permission = $this->perm->checkRight(
117  $gperm_name,
118  $gperm_itemid,
119  $gperm_groupid,
120  $this->mid
121  );
122  if (!$permission) {
123  $helper = Helper::getHelper($this->dirname);
124  $helper->redirect($url, $time, $message);
125  }
126  }
127 
136  public function getGroupsForItem($gperm_name, $gperm_itemid)
137  {
138  return $this->perm->getGroupIds($gperm_name, $gperm_itemid, $this->mid);
139  }
140 
150  public function savePermissionForItem($gperm_name, $gperm_itemid, $groups)
151  {
152  $result = true;
153 
154  // First, delete any existing permissions for this name and id
155  $this->deletePermissionForItem($gperm_name, $gperm_itemid);
156 
157  // Save the new permissions
158  if (count($groups) > 0) {
159  foreach ($groups as $group_id) {
160  $this->perm->addRight(
161  $gperm_name,
162  $gperm_itemid,
163  $group_id,
164  $this->mid
165  );
166  }
167  }
168 
169  return $result;
170  }
171 
180  public function deletePermissionForItem($gperm_name, $gperm_itemid)
181  {
182  return $this->perm->deleteByModule($this->mid, $gperm_name, $gperm_itemid);
183  }
184 
200  public function getGroupSelectFormForItem(
201  $gperm_name,
202  $gperm_itemid,
203  $caption,
204  $name = null,
205  $include_anon = false,
206  $size = 5,
207  $multiple = true
208  ) {
209  if (empty($name)) {
210  $name = $this->defaultFieldName($gperm_name, $gperm_itemid);
211  }
212  $value = $this->getGroupsForItem($gperm_name, $gperm_itemid);
213  $element = new \Xoops\Form\SelectGroup(
214  $caption,
215  $name,
216  $include_anon,
217  $value,
218  $size,
219  $multiple
220  );
221 
222  return $element;
223 
224  }
225 
235  public function defaultFieldName($gperm_name, $gperm_itemid)
236  {
237  $name = $this->module->getVar('dirname') . '_' .
238  $gperm_name . '_' . $gperm_itemid;
239 
240  return $name;
241  }
242 }
static getHelper($dirname= 'system')
Definition: Helper.php:41
getGroupsForItem($gperm_name, $gperm_itemid)
Definition: Permission.php:136
static getInstance()
Definition: Xoops.php:160
static loadFile($file, $once=true)
Definition: Loader.php:36
defaultFieldName($gperm_name, $gperm_itemid)
Definition: Permission.php:235
$result
Definition: pda.php:33
checkPermissionRedirect($gperm_name, $gperm_itemid, $url, $time=3, $message= '')
Definition: Permission.php:108
savePermissionForItem($gperm_name, $gperm_itemid, $groups)
Definition: Permission.php:150
checkPermission($gperm_name, $gperm_itemid)
Definition: Permission.php:85
static get($name)
static getConnection($options=null)
Definition: Factory.php:51
$groups
deletePermissionForItem($gperm_name, $gperm_itemid)
Definition: Permission.php:180
$helper
$url
Definition: register.php:72
getGroupSelectFormForItem($gperm_name, $gperm_itemid, $caption, $name=null, $include_anon=false, $size=5, $multiple=true)
Definition: Permission.php:200