00001 <?php
00002 include_once(GALAXIA_LIBRARY.'/src/ProcessManager/BaseManager.php');
00005
00017 class RoleManager extends BaseManager {
00018
00023 function RoleManager($db)
00024 {
00025 if(!$db) {
00026 die("Invalid db object passed to RoleManager constructor");
00027 }
00028 $this->db = $db;
00029 }
00030
00031 function get_role_id($pid,$name)
00032 {
00033 $name = addslashes($name);
00034 return ($this->getOne("select roleId from ".GALAXIA_TABLE_PREFIX."roles where name='$name' and pId=$pid"));
00035 }
00036
00040 function get_role($pId, $roleId)
00041 {
00042 $query = "select * from `".GALAXIA_TABLE_PREFIX."roles` where `pId`=? and `roleId`=?";
00043 $result = $this->query($query,array($pId, $roleId));
00044 $res = $result->fetchRow();
00045 return $res;
00046 }
00047
00051 function role_name_exists($pid,$name)
00052 {
00053 $name = addslashes($name);
00054 return ($this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."roles where pId=$pid and name='$name'"));
00055 }
00056
00060 function map_user_to_role($pId,$user,$roleId)
00061 {
00062 $query = "delete from `".GALAXIA_TABLE_PREFIX."user_roles` where `roleId`=? and `user`=?";
00063 $this->query($query,array($roleId, $user));
00064 $query = "insert into `".GALAXIA_TABLE_PREFIX."user_roles`(`pId`, `user`, `roleId`) values(?,?,?)";
00065 $this->query($query,array($pId,$user,$roleId));
00066 }
00067
00071 function remove_mapping($user,$roleId)
00072 {
00073 $query = "delete from `".GALAXIA_TABLE_PREFIX."user_roles` where `user`=? and `roleId`=?";
00074 $this->query($query,array($user, $roleId));
00075 }
00076
00080 function list_mappings($pId,$offset,$maxRecords,$sort_mode,$find) {
00081 $sort_mode = $this->convert_sortmode($sort_mode);
00082 if($find) {
00083
00084 $findesc = '%'.$find.'%';
00085 $query = "select `name`,`gr`.`roleId`,`user` from `".GALAXIA_TABLE_PREFIX."roles` gr, `".GALAXIA_TABLE_PREFIX."user_roles` gur where `gr`.`roleId`=`gur`.`roleId` and `gur`.`pId`=? and ((`name` like ?) or (`user` like ?) or (`description` like ?)) order by $sort_mode";
00086 $result = $this->query($query,array($pId,$findesc,$findesc,$findesc), $maxRecords, $offset);
00087 $query_cant = "select count(*) from `".GALAXIA_TABLE_PREFIX."roles` gr, `".GALAXIA_TABLE_PREFIX."user_roles` gur where `gr`.`roleId`=`gur`.`roleId` and `gur`.`pId`=? and ((`name` like ?) or (`user` like ?) or (`description` like ?))";
00088 $cant = $this->getOne($query_cant,array($pId,$findesc,$findesc,$findesc));
00089 } else {
00090 $query = "select `name`,`gr`.`roleId`,`user` from `".GALAXIA_TABLE_PREFIX."roles` gr, `".GALAXIA_TABLE_PREFIX."user_roles` gur where `gr`.`roleId`=`gur`.`roleId` and `gur`.`pId`=? order by $sort_mode";
00091 $result = $this->query($query,array($pId), $maxRecords, $offset);
00092 $query_cant = "select count(*) from `".GALAXIA_TABLE_PREFIX."roles` gr, `".GALAXIA_TABLE_PREFIX."user_roles` gur where `gr`.`roleId`=`gur`.`roleId` and `gur`.`pId`=?";
00093 $cant = $this->getOne($query_cant,array($pId));
00094 }
00095 $ret = Array();
00096 while($res = $result->fetchRow()) {
00097 $ret[] = $res;
00098 }
00099 $retval = Array();
00100 $retval["data"] = $ret;
00101 $retval["cant"] = $cant;
00102 return $retval;
00103 }
00104
00108 function list_roles($pId,$offset,$maxRecords,$sort_mode,$find,$where='')
00109 {
00110 $sort_mode = $this->convert_sortmode($sort_mode);
00111 if($find) {
00112
00113 $findesc = '%'.$find.'%';
00114 $mid=" where pId=? and ((name like ?) or (description like ?))";
00115 $bindvars = array($pId,$findesc,$findesc);
00116 } else {
00117 $mid=" where pId=? ";
00118 $bindvars = array($pId);
00119 }
00120 if($where) {
00121 $mid.= " and ($where) ";
00122 }
00123 $query = "select * from ".GALAXIA_TABLE_PREFIX."roles $mid order by $sort_mode";
00124 $query_cant = "select count(*) from ".GALAXIA_TABLE_PREFIX."roles $mid";
00125 $result = $this->query($query,$bindvars,$maxRecords,$offset);
00126 $cant = $this->getOne($query_cant,$bindvars);
00127 $ret = Array();
00128 while($res = $result->fetchRow()) {
00129 $ret[] = $res;
00130 }
00131 $retval = Array();
00132 $retval["data"] = $ret;
00133 $retval["cant"] = $cant;
00134 return $retval;
00135 }
00136
00137
00138
00142 function remove_role($pId, $roleId)
00143 {
00144 $query = "delete from `".GALAXIA_TABLE_PREFIX."roles` where `pId`=? and `roleId`=?";
00145 $this->query($query,array($pId, $roleId));
00146 $query = "delete from `".GALAXIA_TABLE_PREFIX."activity_roles` where `roleId`=?";
00147 $this->query($query,array($roleId));
00148 $query = "delete from `".GALAXIA_TABLE_PREFIX."user_roles` where `roleId`=?";
00149 $this->query($query,array($roleId));
00150 }
00151
00158 function replace_role($pId, $roleId, $vars)
00159 {
00160 $TABLE_NAME = GALAXIA_TABLE_PREFIX."roles";
00161 $now = date("U");
00162 $vars['lastModif']=$now;
00163 $vars['pId']=$pId;
00164
00165 foreach($vars as $key=>$value)
00166 {
00167 $vars[$key]=addslashes($value);
00168 }
00169
00170 if($roleId) {
00171
00172 $first = true;
00173 $query ="update $TABLE_NAME set";
00174 foreach($vars as $key=>$value) {
00175 if(!$first) $query.= ',';
00176 if(!is_numeric($value)) $value="'".$value."'";
00177 $query.= " $key=$value ";
00178 $first = false;
00179 }
00180 $query .= " where pId=$pId and roleId=$roleId ";
00181 $this->query($query);
00182 } else {
00183 $name = $vars['name'];
00184 if ($this->getOne("select count(*) from ".GALAXIA_TABLE_PREFIX."roles where pId=$pId and name='$name'")) {
00185 return false;
00186 }
00187 unset($vars['roleId']);
00188
00189 $first = true;
00190 $query = "insert into $TABLE_NAME(";
00191 foreach(array_keys($vars) as $key) {
00192 if(!$first) $query.= ',';
00193 $query.= "$key";
00194 $first = false;
00195 }
00196 $query .=") values(";
00197 $first = true;
00198 foreach(array_values($vars) as $value) {
00199 if(!$first) $query.= ',';
00200 if(!is_numeric($value)) $value="'".$value."'";
00201 $query.= "$value";
00202 $first = false;
00203 }
00204 $query .=")";
00205 $this->query($query);
00206 $roleId = $this->getOne("select max(roleId) from $TABLE_NAME where pId=$pId and lastModif=$now");
00207 }
00208
00209 return $roleId;
00210 }
00211 }
00212
00213 ?>