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\Module\Helper;
15: use Xmf\Module\Helper\AbstractHelper;
16: use Xoops\Core\Handler\Factory;
17: use Xoops\Form\SelectGroup;
18:
19: /**
20: * Methods to help manage permissions within a module
21: *
22: * @category Xmf\Module\Helper\Permission
23: * @package Xmf
24: * @author trabis <lusopoemas@gmail.com>
25: * @author Richard Griffith <richard@geekwright.com>
26: * @copyright 2011-2015 XOOPS Project (http://xoops.org)
27: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
28: * @version Release: 1.0
29: * @link http://xoops.org
30: * @since 1.0
31: */
32: class Permission extends AbstractHelper
33: {
34: /**
35: * @var int
36: */
37: private $mid;
38:
39: /**
40: * @var string
41: */
42: private $dirname;
43:
44: /**
45: * @var \Xoops\Core\Kernel\Handlers\XoopsGroupPermHandler
46: */
47: private $permissionHandler;
48:
49: /**
50: * Initialize parent::__constuct calls this after verifying module object.
51: *
52: * @return void
53: */
54: public function init()
55: {
56: $this->mid = $this->module->getVar('mid');
57: $this->dirname = $this->module->getVar('dirname');
58: $this->permissionHandler = Factory::newSpec()->scheme('kernel')->name('groupperm')->build();
59: }
60:
61: /**
62: * Check if the user has permission for an item
63: *
64: * @param string $gperm_name name of the permission to test
65: * @param int $gperm_itemid id of the object to check
66: *
67: * @return bool true if user has access, false if not
68: **/
69: public function checkPermission($gperm_name, $gperm_itemid)
70: {
71: $gperm_groupid = \Xoops::getInstance()->getUserGroups();
72:
73: return $this->permissionHandler->checkRight(
74: $gperm_name,
75: $gperm_itemid,
76: $gperm_groupid,
77: $this->mid
78: );
79: }
80:
81: /**
82: * Redirect to a url if user does not have permission for an item
83: *
84: * @param string $gperm_name name of the permission to test
85: * @param int $gperm_itemid id of the object to check
86: * @param string $url module relative url to redirect to
87: * @param int $time time in seconds to delay
88: * @param string $message message to display with redirect
89: *
90: * @return void
91: **/
92: public function checkPermissionRedirect(
93: $gperm_name,
94: $gperm_itemid,
95: $url,
96: $time = 3,
97: $message = ''
98: ) {
99: $gperm_groupid = \Xoops::getInstance()->getUserGroups();
100: $permission = $this->permissionHandler->checkRight(
101: $gperm_name,
102: $gperm_itemid,
103: $gperm_groupid,
104: $this->mid
105: );
106: if (!$permission) {
107: $helper = Helper::getHelper($this->dirname);
108: $helper->redirect($url, $time, $message);
109: }
110: }
111:
112: /**
113: * Get array of groups with named permission to an item
114: *
115: * @param string $gperm_name name of the permission to test
116: * @param int $gperm_itemid id of the object to check
117: *
118: * @return array groups with permission for item
119: **/
120: public function getGroupsForItem($gperm_name, $gperm_itemid)
121: {
122: return $this->permissionHandler->getGroupIds($gperm_name, $gperm_itemid, $this->mid);
123: }
124:
125: /**
126: * Save group permissions for an item
127: *
128: * @param string $gperm_name name of the permission to test
129: * @param int $gperm_itemid id of the object to check
130: * @param array $groups group ids to grant permission to
131: *
132: * @return bool true if no errors
133: **/
134: public function savePermissionForItem($gperm_name, $gperm_itemid, $groups)
135: {
136: $result = true;
137:
138: // First, delete any existing permissions for this name and id
139: $this->deletePermissionForItem($gperm_name, $gperm_itemid);
140:
141: // Save the new permissions
142: if (count($groups) > 0) {
143: foreach ($groups as $group_id) {
144: $this->permissionHandler->addRight(
145: $gperm_name,
146: $gperm_itemid,
147: $group_id,
148: $this->mid
149: );
150: }
151: }
152:
153: return $result;
154: }
155:
156: /**
157: * Delete all permissions for a specific name and item
158: *
159: * @param string $gperm_name name of the permission to test
160: * @param int $gperm_itemid id of the object to check
161: *
162: * @return bool true if no errors
163: */
164: public function deletePermissionForItem($gperm_name, $gperm_itemid)
165: {
166: return $this->permissionHandler->deleteByModule($this->mid, $gperm_name, $gperm_itemid);
167: }
168:
169: /**
170: * Generate a \Xoops\Form\Element to select groups to grant permission
171: * to a specific gperm_name and gperm_item. Field will be preset
172: * with existing permissions.
173: *
174: * @param string $gperm_name name of the permission to test
175: * @param int $gperm_itemid id of the object to check
176: * @param string $caption caption for form field
177: * @param string $name name/id of form field
178: * @param bool $include_anon true to include anonymous group
179: * @param int $size size of list
180: * @param bool $multiple true to allow multiple selections
181: *
182: * @return SelectGroup
183: */
184: public function getGroupSelectFormForItem(
185: $gperm_name,
186: $gperm_itemid,
187: $caption,
188: $name = null,
189: $include_anon = false,
190: $size = 5,
191: $multiple = true
192: ) {
193: if (empty($name)) {
194: $name = $this->defaultFieldName($gperm_name, $gperm_itemid);
195: }
196: $value = $this->getGroupsForItem($gperm_name, $gperm_itemid);
197: $element = new SelectGroup(
198: $caption,
199: $name,
200: $include_anon,
201: $value,
202: $size,
203: $multiple
204: );
205:
206: return $element;
207:
208: }
209:
210: /**
211: * Generate a default name for a Xoops\Form\SelectGroup based on
212: * module, gperm_name and gperm_itemid
213: *
214: * @param string $gperm_name name of the permission to test
215: * @param int $gperm_itemid id of the object to check
216: *
217: * @return string
218: */
219: public function defaultFieldName($gperm_name, $gperm_itemid)
220: {
221: $name = $this->module->getVar('dirname') . '_' .
222: $gperm_name . '_' . $gperm_itemid;
223:
224: return $name;
225: }
226: }
227: