[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/workflow/inc/engine/src/ProcessManager/ -> RoleManager.php (source)

   1  <?php
   2  
   3  require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'ProcessManager'.SEP.'BaseManager.php');
   4  //!! RoleManager
   5  //! A class to manipulate roles.
   6  /*!
   7    This class is used to add,remove,modify and list
   8    roles used in the Workflow engine.
   9    Roles are managed in a per-process level, each
  10    role belongs to some process.
  11  */
  12  
  13  /*!TODO
  14    Add a method to check if a role name exists in a process (to be used
  15    to prevent duplicate names)
  16  */
  17  
  18  class RoleManager extends BaseManager {
  19      
  20    /*!
  21    *  Constructor takes a PEAR::Db object to be used
  22    *  to manipulate roles in the database.
  23    */
  24    function RoleManager(&$db) 
  25    {
  26      parent::Base($db);
  27      $this->child_name = 'RoleManager';
  28    }
  29  
  30    function get_role_id($pid,$name)
  31    {
  32      $name = addslashes($name);
  33      return ($this->getOne('select wf_role_id from '.GALAXIA_TABLE_PREFIX.'roles where wf_name=? and wf_p_id=?', array($name, $pid)));
  34    }
  35    
  36    /*!
  37    * Gets a role
  38    * @param $pId is the process Id
  39    * @param $roleId is the role Id
  40    * @return fields are returned as an associative array
  41    */
  42    function get_role($pId, $roleId)
  43    {
  44      $query = 'select * from `'.GALAXIA_TABLE_PREFIX.'roles` where `wf_p_id`=? and `wf_role_id`=?';
  45      $result = $this->query($query,array($pId, $roleId));
  46      $res = $result->fetchRow();
  47      return $res;
  48    }
  49    
  50    /*!
  51    * Indicates if a role exists
  52    * @param $pid is the process Id
  53    * @param $name is the name of the role
  54    * @return the number of roles with this name on this process
  55    */
  56    function role_name_exists($pid,$name)
  57    {
  58      $name = addslashes($name);
  59      return ($this->getOne('select count(*) from '.GALAXIA_TABLE_PREFIX.'roles where wf_p_id=? and wf_name=?', array($pid, $name)));
  60    }
  61    
  62    /*!
  63      Maps a user to a role
  64    */
  65    function map_user_to_role($pId,$user,$roleId,$account_type='u')
  66    {
  67    $query = 'delete from `'.GALAXIA_TABLE_PREFIX.'user_roles` where wf_p_id=? AND wf_account_type=? and `wf_role_id`=? and `wf_user`=?';
  68    $this->query($query,array($pId, $account_type,$roleId, $user));
  69    $query = 'insert into '.GALAXIA_TABLE_PREFIX.'user_roles (wf_p_id, wf_user, wf_role_id ,wf_account_type)
  70    values(?,?,?,?)';
  71    $this->query($query,array($pId,$user,$roleId,$account_type));
  72    }
  73    
  74    /*!
  75      Removes a mapping
  76    */
  77    function remove_mapping($user,$roleId)
  78    { 
  79      $query = 'delete from `'.GALAXIA_TABLE_PREFIX.'user_roles` where `wf_user`=? and `wf_role_id`=?';
  80      $this->query($query,array($user, $roleId));
  81    }
  82  
  83    //! Removes an user from the role mappings
  84    /*!
  85    * This function delete all existing mappings concerning one user
  86    * @param $user is the user id
  87    */
  88    function remove_user($user)  
  89    {
  90      $query = 'delete from '.GALAXIA_TABLE_PREFIX.'user_roles where wf_user=?';
  91      $this->query($query,array($user));
  92    }
  93    
  94    //! Transfer all roles from an user to another one
  95    /*!
  96    * This function transfer all existing mappings concerning one user to another user
  97    * @param $user_array is an associative arrays, keys are:
  98    *     * 'old_user' : the actual user id
  99    *     * 'new_user' : the new user id
 100    */
 101    function transfer_user($user_array)  
 102    {
 103      $query = 'update '.GALAXIA_TABLE_PREFIX.'user_roles set wf_user=? where wf_user=?';
 104      $this->query($query,array($user_array['new_user'], $user_array['old_user']));
 105    }
 106    
 107    //!  List mappings
 108    /*!
 109    * get a list of roles/users mappings for a given process.
 110    * @param $pId Process Id. The mappings are returned for a complete process.
 111    * @param $offset first record of the returned array
 112    * @param $maxRecords maximum number of records for the returned array
 113    * @param $sort_mode sort order for the query, like 'wf_name__ASC'
 114    * @param $find searched string in role name, role description or user/group name
 115    * @return an array containg for each row [wf_name] (role name),[wf_role_id],[wf_user] and [wf_account_type] ('u' user  or 'g' group)
 116    * warning: you can have the same user or group several time if mapped to several roles.
 117    */
 118    function list_mappings($pId,$offset,$maxRecords,$sort_mode,$find)  {
 119      $sort_mode = $this->convert_sortmode($sort_mode);
 120      $whereand = ' and gur.wf_p_id=? ';
 121      $bindvars = Array($pId);
 122      if($find) 
 123      {
 124        // no more quoting here - this is done in bind vars already
 125        $findesc = '%'.$find.'%';
 126        $whereand .=  ' and ((wf_name like ?) or (wf_user like ?) or (wf_description like ?)) ';
 127        $bindvars[] = $findesc;
 128        $bindvars[] = $findesc;
 129        $bindvars[] = $findesc;
 130      }
 131      
 132      $query = "select wf_name,gr.wf_role_id,wf_user,wf_account_type from
 133                      ".GALAXIA_TABLE_PREFIX."roles gr,
 134                      ".GALAXIA_TABLE_PREFIX."user_roles gur 
 135                  where gr.wf_role_id=gur.wf_role_id 
 136                  $whereand";
 137      $result = $this->query($query,$bindvars, $maxRecords, $offset, true, $sort_mode);
 138      $query_cant = "select count(*) from 
 139                        ".GALAXIA_TABLE_PREFIX."roles gr, 
 140                        ".GALAXIA_TABLE_PREFIX."user_roles gur 
 141                    where gr.wf_role_id=gur.wf_role_id 
 142                    $whereand";
 143      $cant = $this->getOne($query_cant,$bindvars);
 144  
 145      $ret = Array();
 146      while($res = $result->fetchRow()) {
 147        $ret[] = $res;
 148      }
 149      $retval = Array();
 150      $retval["data"] = $ret;
 151      $retval["cant"] = $cant;
 152      return $retval;
 153    }
 154  
 155    //!  List users/groups mapped on a process, this can be restricted to some activities
 156    /*!
 157    * Get a list of users/groups mapped for a given process. Can expand groups to real users in the result and can restrict
 158    * the mappings to a given subset of roles and or activities
 159    * @param $pId Process Id. The mappings are returned for a complete process by default (see param roles_subset or activities_subset).
 160    * @param $expand_groups if true (false by default) we are not giving the group mappings but instead expand
 161    *     theses groups to real users while avoiding repeating users twice.
 162    * @param $subset associative array containing a list of roles and/or activities for which we want to restrict the list. 
 163    * empty by default.
 164    * This array need to contains the [wf_role_name] key with role names values to restrict roles.
 165    * This array need to contains the [wf_activity_name] key with activity names values to restrict activities.
 166    * @return an array containg for each row the user or group id and an associated name
 167    */
 168  
 169    function &list_mapped_users($pId,$expand_groups=false, $subset=Array())  
 170    {
 171      $whereand = ' where gur.wf_p_id=? ';
 172      $bindvars = Array($pId);
 173      
 174      if (!(count($subset)==0))
 175      {
 176         $roles_subset = Array();
 177         $activities_subset =Array();
 178         foreach($subset as $key => $value )
 179         {
 180           if ($key=='wf_role_name')
 181           {
 182             $roles_subset = $value;
 183           }
 184           if ($key=='wf_activity_name')
 185           {
 186             $activities_subset = $value;
 187           }
 188         }
 189         if (count($roles_subset)>0)
 190         {
 191           if (!(is_array($roles_subset)))
 192           {
 193             $roles_subset = explode(',',$roles_subset);
 194           }
 195           $whereand .= " and ((gr.wf_name) in ('".implode("','",$roles_subset)."'))";
 196         }
 197         if (count($activities_subset)>0)
 198         {
 199           if (!(is_array($activities_subset)))
 200           {
 201             $activities_subset = explode(',',$activities_subset);
 202           }
 203           $whereand .= " and ((ga.wf_name) in ('".implode("','",$activities_subset)."'))";
 204         }
 205      }
 206      $query = "select distinct(wf_user),wf_account_type from
 207                      ".GALAXIA_TABLE_PREFIX."roles gr
 208                  INNER JOIN ".GALAXIA_TABLE_PREFIX."user_roles gur ON gr.wf_role_id=gur.wf_role_id 
 209                  LEFT JOIN ".GALAXIA_TABLE_PREFIX."activity_roles gar ON gar.wf_role_id=gr.wf_role_id 
 210                  LEFT JOIN ".GALAXIA_TABLE_PREFIX."activities ga ON ga.wf_activity_id=gar.wf_activity_id 
 211                  $whereand ";
 212      $result = $this->query($query,$bindvars);
 213      $ret = Array();
 214      if (!(empty($result)))
 215      {
 216        while($res = $result->fetchRow()) 
 217        {
 218          if (($expand_groups) && ($res['wf_account_type']=='g'))
 219          {
 220            //we have a group instead of a simple user and we want real users
 221            $real_users = galaxia_retrieve_group_users($res['wf_user'], true);
 222            foreach ($real_users as $key => $value)
 223            {
 224              $ret[$key]=$value;
 225            }
 226          }
 227          else
 228          {
 229            $ret[$res['wf_user']] = galaxia_retrieve_name($res['wf_user']);
 230          }
 231        }
 232      }
 233      return $ret;
 234    }
 235    
 236    /*!
 237      Lists roles at a per-process level
 238    */
 239    function list_roles($pId,$offset,$maxRecords,$sort_mode,$find,$where='')
 240    {
 241      $sort_mode = $this->convert_sortmode($sort_mode);
 242      if($find) {
 243        // no more quoting here - this is done in bind vars already
 244        $findesc = '%'.$find.'%';
 245        $mid=' where wf_p_id=? and ((wf_name like ?) or (wf_description like ?))';
 246        $bindvars = array($pId,$findesc,$findesc);
 247      } else {
 248        $mid=' where wf_p_id=? ';
 249        $bindvars = array($pId);
 250      }
 251      if($where) {
 252        $mid.= " and ($where) ";
 253      }
 254      $query = 'select * from '.GALAXIA_TABLE_PREFIX."roles $mid";
 255      $query_cant = 'select count(*) from '.GALAXIA_TABLE_PREFIX."roles $mid";
 256      $result = $this->query($query,$bindvars,$maxRecords,$offset, 1, $sort_mode);
 257      $cant = $this->getOne($query_cant,$bindvars);
 258      $ret = Array();
 259      while($res = $result->fetchRow()) {
 260        $ret[] = $res;
 261      }
 262      $retval = Array();
 263      $retval['data'] = $ret;
 264      $retval['cant'] = $cant;
 265      return $retval;
 266    }
 267     
 268    /*! 
 269    * Removes a role.
 270    * @param $pId is the process Id
 271    * @param $roleId is the role Id
 272    * @return true if everything was ok, false in the other case
 273    */
 274    function remove_role($pId, $roleId)
 275    {
 276      // start a transaction
 277      $this->db->StartTrans();
 278      $query = 'delete from `'.GALAXIA_TABLE_PREFIX.'roles` where `wf_p_id`=? and `wf_role_id`=?';
 279      $this->query($query,array($pId, $roleId));
 280      $query = 'delete from `'.GALAXIA_TABLE_PREFIX.'activity_roles` where `wf_role_id`=?';
 281      $this->query($query,array($roleId));
 282      $query = 'delete from `'.GALAXIA_TABLE_PREFIX.'user_roles` where `wf_role_id`=?';
 283      $this->query($query,array($roleId));
 284      // perform commit (return true) or Rollback (return false)
 285      return $this->db->CompleteTrans();
 286      }
 287    
 288    /*!
 289    * Updates or inserts a new role in the database, 
 290    * @param $vars is an associative array containing the fields to update or to insert as needed.
 291    * @param $pId is the processId
 292    * @param $roleId is the roleId, 0 in insert mode
 293    * @return the roleId (the new one if in insert mode) if everything was ok, false in the other case
 294    */
 295    function replace_role($pId, $roleId, $vars) 
 296    {
 297      // start a transaction 
 298      $this->db->StartTrans(); 
 299      $TABLE_NAME = GALAXIA_TABLE_PREFIX.'roles'; 
 300      $now = date("U");
 301      if (!(isset($vars['wf_last_modif']))) $vars['wf_last_modif']=$now; 
 302      $vars['wf_p_id']=$pId;
 303      
 304      foreach($vars as $key=>$value) 
 305      {
 306        $vars[$key]=addslashes($value);
 307      }
 308    
 309      if($roleId) {
 310        // update mode 
 311        $first = true; 
 312        $query ="update $TABLE_NAME set"; 
 313        $bindvars = Array();
 314        foreach($vars as $key=>$value) 
 315        {
 316          if(!$first) $query.= ','; 
 317          //if(!is_numeric($value)) $value="'".$value."'"; 
 318          $query.= " $key=? ";
 319          $bindvars[] = $value; 
 320          $first = false;
 321        }
 322        $query .= ' where wf_p_id=? and wf_role_id=? '; 
 323        $bindvars[] = $pId;
 324        $bindvars[] = $roleId; 
 325        $this->query($query, $bindvars);
 326      } 
 327      else 
 328      {
 329        //check unicity 
 330        $name = $vars['wf_name']; 
 331        if ($this->getOne('select count(*) from '.$TABLE_NAME.' where wf_p_id=? and wf_name=?', array($pId,$name))) 
 332        { 
 333          return false;
 334        }
 335        unset($vars['wf_role_id']); 
 336        // insert mode
 337        $bindvars = Array();
 338        $first = true;
 339        $query = "insert into $TABLE_NAME(";
 340        foreach(array_keys($vars) as $key)
 341        {
 342          if(!$first) $query.= ','; 
 343          $query.= "$key";
 344          $first = false;
 345        } 
 346        $query .=') values(';
 347        $first = true;
 348        foreach(array_values($vars) as $value) 
 349        {
 350          if(!$first) $query.= ','; 
 351          //if(!is_numeric($value)) $value="'".$value."'";
 352          $query.= '?';
 353          $bindvars[] = $value;
 354          $first = false;
 355        } 
 356        $query .=')';
 357        $this->query($query, $bindvars);
 358        //get the last inserted row
 359        $roleId = $this->getOne('select max(wf_role_id) from '.$TABLE_NAME.' where wf_p_id=?', array($pId)); 
 360      }
 361      // perform commit (return true) or Rollback (return false)
 362      if ($this->db->CompleteTrans())
 363      {
 364        // Get the id
 365        return $roleId;
 366      }
 367      else
 368      {
 369        return false;
 370      }
 371    }
 372    
 373    /*!
 374    * List all users and groups recorded in the mappings with their status (user or group)
 375    * @return an associative array containing a row for each user. This row is an array
 376    * containing 'wf_user' and 'wf_account_type' keys.
 377    */
 378    function get_all_users()
 379    {
 380      $final = Array();
 381      //query for user mappings affected to groups & vice-versa
 382      $query ='select distinct(gur.wf_user), gur.wf_account_type
 383              from '.GALAXIA_TABLE_PREFIX.'user_roles gur';
 384      $result = $this->query($query);
 385      if (!(empty($result)))
 386      {
 387      while ($res = $result->fetchRow())
 388      {
 389          $final[] = $res;
 390      }
 391      }
 392      return $final;
 393    }
 394  
 395  }
 396  
 397  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7