source: temp/test-xoops.ec-cube.net/html/kernel/groupperm.php @ 405

Revision 405, 14.2 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: groupperm.php,v 1.3 2006/05/01 02:37:28 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29// Project: The XOOPS Project                                                //
30// ------------------------------------------------------------------------- //
31
32if (!defined('XOOPS_ROOT_PATH')) {
33    exit();
34}
35
36/**
37 *
38 *
39 * @package     kernel
40 *
41 * @author      Kazumi Ono  <[email protected]>
42 * @copyright   copyright (c) 2000-2003 XOOPS.org
43 */
44
45/**
46 * A group permission
47 *
48 * These permissions are managed through a {@link XoopsGroupPermHandler} object
49 *
50 * @package     kernel
51 *
52 * @author      Kazumi Ono  <[email protected]>
53 * @copyright   copyright (c) 2000-2003 XOOPS.org
54 */
55class XoopsGroupPerm extends XoopsObject
56{
57
58    /**
59     * Constructor
60     *
61     */
62    function XoopsGroupPerm()
63    {
64        $this->XoopsObject();
65        $this->initVar('gperm_id', XOBJ_DTYPE_INT, null, false);
66        $this->initVar('gperm_groupid', XOBJ_DTYPE_INT, null, false);
67        $this->initVar('gperm_itemid', XOBJ_DTYPE_INT, null, false);
68        $this->initVar('gperm_modid', XOBJ_DTYPE_INT, 0, false);
69        $this->initVar('gperm_name', XOBJ_DTYPE_OTHER, null, false);
70    }
71}
72
73
74/**
75* XOOPS group permission handler class.
76*
77* This class is responsible for providing data access mechanisms to the data source
78* of XOOPS group permission class objects.
79* This class is an abstract class to be implemented by child group permission classes.
80*
81* @see          XoopsGroupPerm
82* @author       Kazumi Ono  <[email protected]>
83* @copyright    copyright (c) 2000-2003 XOOPS.org
84*/
85class XoopsGroupPermHandler extends XoopsObjectHandler
86{
87
88    /**
89     * Create a new {@link XoopsGroupPerm}
90     *
91     * @return  bool    $isNew  Flag the object as "new"?
92     */
93    function &create($isNew = true)
94    {
95        $perm =& new XoopsGroupPerm();
96        if ($isNew) {
97            $perm->setNew();
98        }
99        return $perm;
100    }
101
102    /**
103     * Retrieve a group permission
104     *
105     * @param   int $id ID
106     *
107     * @return  object  {@link XoopsGroupPerm}, FALSE on fail
108     */
109    function &get($id)
110    {
111        $ret = false;
112        if (intval($id) > 0) {
113            $sql = sprintf("SELECT * FROM %s WHERE gperm_id = %u", $this->db->prefix('group_permission'), $id);
114            if ($result = $this->db->query($sql)) {
115                $numrows = $this->db->getRowsNum($result);
116                if ( $numrows == 1 ) {
117                    $perm =& new XoopsGroupPerm();
118                    $perm->assignVars($this->db->fetchArray($result));
119                    $ret =& $perm;
120                }
121            }
122        }
123        return $ret;
124    }
125
126    /**
127     * Store a {@link XoopsGroupPerm}
128     *
129     * @param   object  &$perm  {@link XoopsGroupPerm} object
130     *
131     * @return  bool    TRUE on success
132     */
133    function insert(&$perm)
134    {
135        if ( strtolower(get_class($perm)) != 'xoopsgroupperm' ) {
136            return false;
137        }
138        if ( !$perm->isDirty() ) {
139            return true;
140        }
141        if (!$perm->cleanVars()) {
142            return false;
143        }
144        foreach ($perm->cleanVars as $k => $v) {
145            ${$k} = $v;
146        }
147        if ($perm->isNew()) {
148            $gperm_id = $this->db->genId('group_permission_gperm_id_seq');
149            $sql = sprintf("INSERT INTO %s (gperm_id, gperm_groupid, gperm_itemid, gperm_modid, gperm_name) VALUES (%u, %u, %u, %u, %s)", $this->db->prefix('group_permission'), $gperm_id, $gperm_groupid, $gperm_itemid, $gperm_modid, $this->db->quoteString($gperm_name));
150        } else {
151            $sql = sprintf("UPDATE %s SET gperm_groupid = %u, gperm_itemid = %u, gperm_modid = %u WHERE gperm_id = %u", $this->db->prefix('group_permission'), $gperm_groupid, $gperm_itemid, $gperm_modid, $gperm_id);
152        }
153        if (!$result = $this->db->query($sql)) {
154            return false;
155        }
156        if (empty($gperm_id)) {
157            $gperm_id = $this->db->getInsertId();
158        }
159        $perm->assignVar('gperm_id', $gperm_id);
160        return true;
161    }
162
163    /**
164     * Delete a {@link XoopsGroupPerm}
165     *
166     * @param   object  &$perm 
167     *
168     * @return  bool    TRUE on success
169     */
170    function delete(&$perm)
171    {
172        if (strtolower(get_class($perm)) != 'xoopsgroupperm') {
173            return false;
174        }
175        $sql = sprintf("DELETE FROM %s WHERE gperm_id = %u", $this->db->prefix('group_permission'), $perm->getVar('gperm_id'));
176        if (!$result = $this->db->query($sql)) {
177            return false;
178        }
179        return true;
180    }
181
182    /**
183     * Retrieve multiple {@link XoopsGroupPerm}s
184     *
185     * @param   object  $criteria   {@link CriteriaElement}
186     * @param   bool    $id_as_key  Use IDs as array keys?
187     *
188     * @return  array   Array of {@link XoopsGroupPerm}s
189     */
190    function &getObjects($criteria = null, $id_as_key = false)
191    {
192        $ret = array();
193        $limit = $start = 0;
194        $sql = 'SELECT * FROM '.$this->db->prefix('group_permission');
195        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
196            $sql .= ' '.$criteria->renderWhere();
197            $limit = $criteria->getLimit();
198            $start = $criteria->getStart();
199        }
200        $result = $this->db->query($sql, $limit, $start);
201        if (!$result) {
202            return $ret;
203        }
204        while ($myrow = $this->db->fetchArray($result)) {
205            $perm =& new XoopsGroupPerm();
206            $perm->assignVars($myrow);
207            if (!$id_as_key) {
208                $ret[] =& $perm;
209            } else {
210                $ret[$myrow['gperm_id']] =& $perm;
211            }
212            unset($perm);
213        }
214        return $ret;
215    }
216
217    /**
218     * Count some {@link XoopsGroupPerm}s
219     *
220     * @param   object  $criteria   {@link CriteriaElement}
221     *
222     * @return  int
223     */
224    function getCount($criteria = null)
225    {
226        $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('group_permission');
227        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
228            $sql .= ' '.$criteria->renderWhere();
229        }
230        $result = $this->db->query($sql);
231        if (!$result) {
232            return 0;
233        }
234        list($count) = $this->db->fetchRow($result);
235        return $count;
236    }
237
238    /**
239     * Delete all permissions by a certain criteria
240     *
241     * @param   object  $criteria   {@link CriteriaElement}
242     *
243     * @return  bool    TRUE on success
244     */
245    function deleteAll($criteria = null)
246    {
247        $sql = sprintf("DELETE FROM %s", $this->db->prefix('group_permission'));        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
248            $sql .= ' '.$criteria->renderWhere();
249        }
250        if (!$result = $this->db->query($sql)) {
251            return false;
252        }
253        return true;
254    }
255
256    /**
257     * Delete all module specific permissions assigned for a group
258     *
259     * @param   int  $gperm_groupid ID of a group
260     * @param   int  $gperm_modid ID of a module
261     *
262     * @return  bool TRUE on success
263     */
264    function deleteByGroup($gperm_groupid, $gperm_modid = null)
265    {
266        $criteria = new CriteriaCompo(new Criteria('gperm_groupid', intval($gperm_groupid)));
267        if (isset($gperm_modid)) {
268            $criteria->add(new Criteria('gperm_modid', intval($gperm_modid)));
269        }
270        return $this->deleteAll($criteria);
271    }
272
273    /**
274     * Delete all module specific permissions
275     *
276     * @param   int  $gperm_modid ID of a module
277     * @param   string  $gperm_name Name of a module permission
278     * @param   int  $gperm_itemid ID of a module item
279     *
280     * @return  bool TRUE on success
281     */
282    function deleteByModule($gperm_modid, $gperm_name = null, $gperm_itemid = null)
283    {
284        $criteria = new CriteriaCompo(new Criteria('gperm_modid', intval($gperm_modid)));
285        if (isset($gperm_name)) {
286            $criteria->add(new Criteria('gperm_name', $gperm_name));
287            if (isset($gperm_itemid)) {
288                $criteria->add(new Criteria('gperm_itemid', intval($gperm_itemid)));
289            }
290        }
291        return $this->deleteAll($criteria);
292    }
293    /**#@-*/
294
295    /**
296     * Check permission
297     *
298     * @param   string    $gperm_name       Name of permission
299     * @param   int       $gperm_itemid     ID of an item
300     * @param   int/array $gperm_groupid    A group ID or an array of group IDs
301     * @param   int       $gperm_modid      ID of a module
302     *
303     * @return  bool    TRUE if permission is enabled
304     */
305    function checkRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
306    {
307        $criteria = new CriteriaCompo(new Criteria('gperm_modid', $gperm_modid));
308        $criteria->add(new Criteria('gperm_name', $gperm_name));
309        $gperm_itemid = intval($gperm_itemid);
310        if ($gperm_itemid > 0) {
311            $criteria->add(new Criteria('gperm_itemid', $gperm_itemid));
312        }
313        if (is_array($gperm_groupid)) {
314            if (in_array(XOOPS_GROUP_ADMIN, $gperm_groupid)) {
315                return true;
316            }
317            $criteria2 = new CriteriaCompo();
318            foreach ($gperm_groupid as $gid) {
319                $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR');
320            }
321            $criteria->add($criteria2);
322        } else {
323            if (XOOPS_GROUP_ADMIN == $gperm_groupid) {
324                return true;
325            }
326            $criteria->add(new Criteria('gperm_groupid', $gperm_groupid));
327        }
328        if ($this->getCount($criteria) > 0) {
329            return true;
330        }
331        return false;
332    }
333
334    /**
335     * Add a permission
336     *
337     * @param   string  $gperm_name       Name of permission
338     * @param   int     $gperm_itemid     ID of an item
339     * @param   int     $gperm_groupid    ID of a group
340     * @param   int     $gperm_modid      ID of a module
341     *
342     * @return  bool    TRUE jf success
343     */
344    function addRight($gperm_name, $gperm_itemid, $gperm_groupid, $gperm_modid = 1)
345    {
346        $perm =& $this->create();
347        $perm->setVar('gperm_name', $gperm_name);
348        $perm->setVar('gperm_groupid', $gperm_groupid);
349        $perm->setVar('gperm_itemid', $gperm_itemid);
350        $perm->setVar('gperm_modid', $gperm_modid);
351        return $this->insert($perm);
352    }
353
354    /**
355     * Get all item IDs that a group is assigned a specific permission
356     *
357     * @param   string    $gperm_name       Name of permission
358     * @param   int/array $gperm_groupid    A group ID or an array of group IDs
359     * @param   int       $gperm_modid      ID of a module
360     *
361     * @return  array     array of item IDs
362     */
363    function getItemIds($gperm_name, $gperm_groupid, $gperm_modid = 1)
364    {
365        $ret = array();
366        $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
367        $criteria->add(new Criteria('gperm_modid', intval($gperm_modid)));
368        if (is_array($gperm_groupid)) {
369            $criteria2 = new CriteriaCompo();
370            foreach ($gperm_groupid as $gid) {
371                $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR');
372            }
373            $criteria->add($criteria2);
374        } else {
375            $criteria->add(new Criteria('gperm_groupid', intval($gperm_groupid)));
376        }
377        $perms =& $this->getObjects($criteria, true);
378        foreach (array_keys($perms) as $i) {
379            $ret[] = $perms[$i]->getVar('gperm_itemid');
380        }
381        return array_unique($ret);
382    }
383
384    /**
385     * Get all group IDs assigned a specific permission for a particular item
386     *
387     * @param   string  $gperm_name       Name of permission
388     * @param   int     $gperm_itemid     ID of an item
389     * @param   int     $gperm_modid      ID of a module
390     *
391     * @return  array   array of group IDs
392     */
393    function getGroupIds($gperm_name, $gperm_itemid, $gperm_modid = 1)
394    {
395        $ret = array();
396        $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name));
397        $criteria->add(new Criteria('gperm_itemid', intval($gperm_itemid)));
398        $criteria->add(new Criteria('gperm_modid', intval($gperm_modid)));
399        $perms =& $this->getObjects($criteria, true);
400        foreach (array_keys($perms) as $i) {
401            $ret[] = $perms[$i]->getVar('gperm_groupid');
402        }
403        return $ret;
404    }
405}
406?>
Note: See TracBrowser for help on using the repository browser.