source: branches/1.2/workflow/inc/engine/src/ProcessManager/RoleManager.php @ 1349

Revision 1349, 15.2 KB checked in by niltonneto, 15 years ago (diff)

Ticket #561 - Inclusão do módulo Workflow faltante nessa versão.

  • Property svn:executable set to *
Line 
1<?php
2require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'ProcessManager'.SEP.'BaseManager.php');
3
4/**
5 * Adds, removes, modifies and lists roles used in the Workflow engine.
6 * Roles are managed in a per-process level, each role belongs to some process
7 *
8 * @package Galaxia
9 * @license http://www.gnu.org/copyleft/gpl.html GPL
10 * @todo Add a method to check if a role name exists in a process to prevent duplicate names
11 */
12class RoleManager extends BaseManager {
13   
14  /**
15   * Constructor
16   *
17   * @param object &$db ADOdb
18   * @return object RoleManager
19   * @access public
20   */
21  function RoleManager(&$db)
22  {
23    parent::Base($db);
24    $this->child_name = 'RoleManager';
25  }
26
27  /**
28   * Gets role id
29   *
30   * @param int $pid Process id
31   * @param string $name Role name
32   * @access public
33   * @return string
34   */
35  function get_role_id($pid,$name)
36  {
37    $name = addslashes($name);
38    return ($this->getOne('select wf_role_id from '.GALAXIA_TABLE_PREFIX.'roles where wf_name=? and wf_p_id=?', array($name, $pid)));
39  }
40 
41  /**
42  * Gets a role
43  *
44  * @param int $pId Process Id
45  * @param int $roleId Role Id
46  * @return array
47  * @access public
48  */
49  function get_role($pId, $roleId)
50  {
51    $query = 'select * from `'.GALAXIA_TABLE_PREFIX.'roles` where `wf_p_id`=? and `wf_role_id`=?';
52    $result = $this->query($query,array($pId, $roleId));
53    $res = $result->fetchRow();
54    return $res;
55  }
56 
57  /**
58  * Indicates if a role exists
59  *
60  * @param int $pid Process Id
61  * @param string $name Role name
62  * @return int Number of roles with this name on this process
63  * @access public
64  */
65  function role_name_exists($pid,$name)
66  {
67    $name = addslashes($name);
68    return ($this->getOne('select count(*) from '.GALAXIA_TABLE_PREFIX.'roles where wf_p_id=? and wf_name=?', array($pid, $name)));
69  }
70 
71  /**
72   * Maps a user to a role
73   *
74   * @param int $pId Process id
75   * @param int $user User id
76   * @param int $roleId Role id
77   * @param string $account_type User account type
78   * @return void
79   * @access public
80   */
81  function map_user_to_role($pId,$user,$roleId,$account_type='u')
82  {
83  $query = 'delete from `'.GALAXIA_TABLE_PREFIX.'user_roles` where wf_p_id=? AND wf_account_type=? and `wf_role_id`=? and `wf_user`=?';
84  $this->query($query,array($pId, $account_type,$roleId, $user));
85  $query = 'insert into '.GALAXIA_TABLE_PREFIX.'user_roles (wf_p_id, wf_user, wf_role_id ,wf_account_type)
86  values(?,?,?,?)';
87  $this->query($query,array($pId,$user,$roleId,$account_type));
88  }
89 
90  /**
91   * Removes a mapping
92   *
93   * @param int $user User id
94   * @param int $roleId Role id
95   * @return void
96   * @access public
97   */
98  function remove_mapping($user,$roleId)
99  {
100    $query = 'delete from `'.GALAXIA_TABLE_PREFIX.'user_roles` where `wf_user`=? and `wf_role_id`=?';
101    $this->query($query,array($user, $roleId));
102  }
103
104  /**
105   * Deletes all existing mappings concerning one user
106   *
107   * @param int $user User id
108   * @return void
109   * @access public
110   */
111  function remove_user($user) 
112  {
113    $query = 'delete from '.GALAXIA_TABLE_PREFIX.'user_roles where wf_user=?';
114    $this->query($query,array($user));
115  }
116 
117  /**
118  * Transfers all existing mappings concerning one user to another user
119  *
120  * @param array $user_array Associative, keys are: 'old_user': current user id and 'new_user', the new user id
121  * @return void
122  * @access public
123  */
124  function transfer_user($user_array) 
125  {
126    $query = 'update '.GALAXIA_TABLE_PREFIX.'user_roles set wf_user=? where wf_user=?';
127    $this->query($query,array($user_array['new_user'], $user_array['old_user']));
128  }
129 
130  /**
131  * Gets list of roles/users mappings for a given process
132  *
133  * @param int $pId Process id, mappings are returned for a complete process
134  * @param int $offset Starting record of the returned array
135  * @param int $maxRecords Maximum number of records for the returned array
136  * @param string $sort_mode Sort order for the query, like 'wf_name__ASC'
137  * @param string $find Searched in role name, role description or user/group name
138  * @return array Having for each row [wf_name] (role name),[wf_role_id],[wf_user] and [wf_account_type] ('u' user  or 'g' group),
139  * Be aware 'cause you may have the same user or group several times if mapped to several roles
140  * @access public
141  */
142  function list_mappings($pId,$offset,$maxRecords,$sort_mode,$find)  {
143    $sort_mode = $this->convert_sortmode($sort_mode);
144    $whereand = ' and gur.wf_p_id=? ';
145    $bindvars = Array($pId);
146    if($find)
147    {
148      // no more quoting here - this is done in bind vars already
149      $findesc = '%'.$find.'%';
150      $whereand .=  ' and ((wf_name like ?) or (wf_user like ?) or (wf_description like ?)) ';
151      $bindvars[] = $findesc;
152      $bindvars[] = $findesc;
153      $bindvars[] = $findesc;
154    }
155   
156    $query = "select wf_name,gr.wf_role_id,wf_user,wf_account_type from
157                    ".GALAXIA_TABLE_PREFIX."roles gr,
158                    ".GALAXIA_TABLE_PREFIX."user_roles gur
159                where gr.wf_role_id=gur.wf_role_id
160                $whereand";
161    $result = $this->query($query,$bindvars, $maxRecords, $offset, true, $sort_mode);
162    $query_cant = "select count(*) from
163                      ".GALAXIA_TABLE_PREFIX."roles gr,
164                      ".GALAXIA_TABLE_PREFIX."user_roles gur
165                  where gr.wf_role_id=gur.wf_role_id
166                  $whereand";
167    $cant = $this->getOne($query_cant,$bindvars);
168
169    $ret = Array();
170    while($res = $result->fetchRow()) {
171      $ret[] = $res;
172    }
173    $retval = Array();
174    $retval["data"] = $ret;
175    $retval["cant"] = $cant;
176    return $retval;
177  }
178
179  /**
180   * Gets a list of users/groups mapped for a given process. Can expand groups to real users in the result and can restrict mappings to a given subset of roles and or activities
181   *
182   * @param int $pId Process id, mappings are returned for a complete process by default (see param roles_subset or activities_subset)
183   * @param bool $expand_groups If true (false by default) we are not giving the group mappings but instead expand these groups to real users while avoiding repeating users twice
184   * @param array $subset Associative containing a list of roles and/or activities for which we want to restrict the list empty by default.
185   * This array needs to contains the [wf_role_name] key with role names values to restrict roles.
186   * This array needs to contains the [wf_activity_name] key with activity names values to restrict activities
187   * @return array Associative, having for each row the user or group id and an associated name
188   * @access public
189   */
190
191  function &list_mapped_users($pId,$expand_groups=false, $subset=Array()) 
192  {
193    $whereand = ' where gur.wf_p_id=? ';
194    $bindvars = Array($pId);
195   
196    if (!(count($subset)==0))
197    {
198       $roles_subset = Array();
199       $activities_subset =Array();
200       $activities_id_subset = Array();
201       foreach($subset as $key => $value )
202       {
203         if ($key=='wf_role_name')
204         {
205           $roles_subset = $value;
206         }
207         if ($key=='wf_activity_name')
208         {
209           $activities_subset = $value;
210         }
211         if ($key == 'wf_activity_id')
212         {
213           $activities_id_subset = $value;
214         }
215       }
216       if (count($roles_subset)>0)
217       {
218         if (!(is_array($roles_subset)))
219         {
220           $roles_subset = explode(',',$roles_subset);
221         }
222         $whereand .= " and ((gr.wf_name) in ('".implode("','",$roles_subset)."'))";
223       }
224       if (count($activities_subset)>0)
225       {
226         if (!(is_array($activities_subset)))
227         {
228           $activities_subset = explode(',',$activities_subset);
229         }
230         $whereand .= " and ((ga.wf_name) in ('".implode("','",$activities_subset)."'))";
231       }
232       if (count($activities_id_subset) > 0)
233       {
234         if (!(is_array($activities_id_subset)))
235         {
236           $activities_id_subset = explode(',',$activities_id_subset);
237         }
238         $whereand .= " and ((ga.wf_activity_id) in ('".implode("','",$activities_id_subset)."'))";
239       }
240    }
241    $query = "select distinct(wf_user),wf_account_type from
242                    ".GALAXIA_TABLE_PREFIX."roles gr
243                INNER JOIN ".GALAXIA_TABLE_PREFIX."user_roles gur ON gr.wf_role_id=gur.wf_role_id
244                LEFT JOIN ".GALAXIA_TABLE_PREFIX."activity_roles gar ON gar.wf_role_id=gr.wf_role_id
245                LEFT JOIN ".GALAXIA_TABLE_PREFIX."activities ga ON ga.wf_activity_id=gar.wf_activity_id
246                $whereand ";
247    $result = $this->query($query,$bindvars);
248    $ret = Array();
249    $ldap = &$GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP');
250    if (!(empty($result)))
251    {
252      while($res = $result->fetchRow())
253      {
254        if (($expand_groups) && ($res['wf_account_type']=='g'))
255        {
256          //we have a group instead of a simple user and we want real users
257          $real_users = galaxia_retrieve_group_users($res['wf_user'], true);
258          foreach ($real_users as $key => $value)
259          {
260            $ret[$key]=$value;
261          }
262        }
263        else
264        {
265          $ret[$res['wf_user']] = $ldap->getName($res['wf_user']);
266        }
267      }
268    }
269    return $ret;
270  }
271 
272  /**
273   * Lists roles at process level
274   *
275   * @param int $pId Process id
276   * @param int $offset Starting resultset row
277   * @param int $maxRecords Maximum number of records
278   * @param string $sort_mode Query sorting mode
279   * @param string $find Search query string
280   * @param string $where Condition query string
281   * @return array Roles list
282   * @access public
283   */
284  function list_roles($pId,$offset,$maxRecords,$sort_mode,$find,$where='')
285  {
286    $sort_mode = $this->convert_sortmode($sort_mode);
287    if($find) {
288      // no more quoting here - this is done in bind vars already
289      $findesc = '%'.$find.'%';
290      $mid=' where wf_p_id=? and ((wf_name like ?) or (wf_description like ?))';
291      $bindvars = array($pId,$findesc,$findesc);
292    } else {
293      $mid=' where wf_p_id=? ';
294      $bindvars = array($pId);
295    }
296    if($where) {
297      $mid.= " and ($where) ";
298    }
299    $query = 'select * from '.GALAXIA_TABLE_PREFIX."roles $mid";
300    $query_cant = 'select count(*) from '.GALAXIA_TABLE_PREFIX."roles $mid";
301    $result = $this->query($query,$bindvars,$maxRecords,$offset, 1, $sort_mode);
302    $cant = $this->getOne($query_cant,$bindvars);
303    $ret = Array();
304    while($res = $result->fetchRow()) {
305      $ret[] = $res;
306    }
307    $retval = Array();
308    $retval['data'] = $ret;
309    $retval['cant'] = $cant;
310    return $retval;
311  }
312   
313  /**
314  * Removes a role
315  *
316  * @param int $pId Process Id
317  * @param int $roleId Role Id
318  * @return bool
319  * @access public
320  */
321  function remove_role($pId, $roleId)
322  {
323    // start a transaction
324    $this->db->StartTrans();
325    $query = 'delete from `'.GALAXIA_TABLE_PREFIX.'roles` where `wf_p_id`=? and `wf_role_id`=?';
326    $this->query($query,array($pId, $roleId));
327    $query = 'delete from `'.GALAXIA_TABLE_PREFIX.'activity_roles` where `wf_role_id`=?';
328    $this->query($query,array($roleId));
329    $query = 'delete from `'.GALAXIA_TABLE_PREFIX.'user_roles` where `wf_role_id`=?';
330    $this->query($query,array($roleId));
331    // perform commit (return true) or Rollback (return false)
332    return $this->db->CompleteTrans();
333    }
334 
335  /**
336  * Updates or inserts a new role in the database
337  * 
338  * @param array $vars Associative, having the fields to update or to insert as needed
339  * @param int $pId Process id
340  * @param int $roleId Role id, 0 in insert mode
341  * @return mixed Role id (the new one if in insert mode) if everything was ok, false in the other case
342  */
343  function replace_role($pId, $roleId, $vars)
344  {
345    // start a transaction
346    $this->db->StartTrans();
347    $TABLE_NAME = GALAXIA_TABLE_PREFIX.'roles';
348    $now = date("U");
349    if (!(isset($vars['wf_last_modif']))) $vars['wf_last_modif']=$now;
350    $vars['wf_p_id']=$pId;
351   
352    foreach($vars as $key=>$value)
353    {
354      $vars[$key]=addslashes($value);
355    }
356 
357    if($roleId) {
358      // update mode
359      $first = true;
360      $query ="update $TABLE_NAME set";
361      $bindvars = Array();
362      foreach($vars as $key=>$value)
363      {
364        if(!$first) $query.= ',';
365        //if(!is_numeric($value)) $value="'".$value."'";
366        $query.= " $key=? ";
367        $bindvars[] = $value;
368        $first = false;
369      }
370      $query .= ' where wf_p_id=? and wf_role_id=? ';
371      $bindvars[] = $pId;
372      $bindvars[] = $roleId;
373      $this->query($query, $bindvars);
374    }
375    else
376    {
377      //check unicity
378      $name = $vars['wf_name'];
379      if ($this->getOne('select count(*) from '.$TABLE_NAME.' where wf_p_id=? and wf_name=?', array($pId,$name)))
380      {
381        return false;
382      }
383      unset($vars['wf_role_id']);
384      // insert mode
385      $bindvars = Array();
386      $first = true;
387      $query = "insert into $TABLE_NAME(";
388      foreach(array_keys($vars) as $key)
389      {
390        if(!$first) $query.= ',';
391        $query.= "$key";
392        $first = false;
393      }
394      $query .=') values(';
395      $first = true;
396      foreach(array_values($vars) as $value)
397      {
398        if(!$first) $query.= ',';
399        //if(!is_numeric($value)) $value="'".$value."'";
400        $query.= '?';
401        $bindvars[] = $value;
402        $first = false;
403      }
404      $query .=')';
405      $this->query($query, $bindvars);
406      //get the last inserted row
407      $roleId = $this->getOne('select max(wf_role_id) from '.$TABLE_NAME.' where wf_p_id=?', array($pId));
408    }
409    // perform commit (return true) or Rollback (return false)
410    if ($this->db->CompleteTrans())
411    {
412      // Get the id
413      return $roleId;
414    }
415    else
416    {
417      return false;
418    }
419  }
420 
421  /**
422  * Lists all users and groups recorded in the mappings with their status (user or group)
423  *
424  * @return array Associative, containing a row for each user, where each row is an array containing 'wf_user' and 'wf_account_type' keys
425  * @access public
426  */
427  function get_all_users()
428  {
429    $final = Array();
430    //query for user mappings affected to groups & vice-versa
431    $query ='select distinct(gur.wf_user), gur.wf_account_type
432            from '.GALAXIA_TABLE_PREFIX.'user_roles gur';
433    $result = $this->query($query);
434    if (!(empty($result)))
435    {
436    while ($res = $result->fetchRow())
437    {
438        $final[] = $res;
439    }
440    }
441    return $final;
442  }
443
444  /**
445   * Get roles for a given user
446   *
447   * @param int $user The given user
448   * @return array Roles for the given user
449   * @access public
450   */
451  function getUserRoles($user, $pid = null)
452  {
453    /* retrieve user_groups information in an array containing all groups for this user */
454    $userGroups = galaxia_retrieve_user_groups($user);
455    $values = array($user);
456    $query = 'SELECT wf_role_id FROM ' . GALAXIA_TABLE_PREFIX . 'user_roles
457          WHERE (
458            (wf_user = ? AND wf_account_type = \'u\')';
459    if (is_array($userGroups))
460    {
461      foreach ($userGroups as &$group)
462        $group = "'{$group}'";
463      $query .= ' OR (wf_user IN (' . implode(',', $userGroups) . ') AND wf_account_type = \'g\')';
464    }
465    $query .= ')';
466    if (!is_null($pid))
467    {
468      $query .= ' AND (wf_p_id = ?)';
469      $values[] = $pid;
470    }
471
472    $result = $this->query($query, $values);
473    $output = Array();
474    while($res = $result->fetchRow())
475      $output[] = $res['wf_role_id'];
476
477    return $output;
478  }
479
480}
481?>
Note: See TracBrowser for help on using the repository browser.