1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
<?php
/**
* The ArtifactWorkflow class manages workflow for trackers.
*
* The workflow is attached to custom status field only.
*
* Associated tables are:
* - artifact_workflow_event : to track allowed events.
* - artifact_workflow_roles : to track roles allowed to perform an event.
* - artifact_workflow_notify : to track notification associated to an event (not implemented).
*
* An event is a transition from one value to another.
*
* NOTE: Code should be improved to manage any kind of custom fields not only of type
* 'Status' but maybe also for the 'select' also.
*
* 2008 : Alain Peyrat <alain.peyrat@alcatel-lucent.com>
*
* NOTES:
* @todo: the getAllowedRoles should be replaced by getRealAllowedRoles code. (to be tested).
* @todo: Some code could use a db direct to array func instead of the while.
*
*/
require_once $gfcommon.'include/Error.class.php';
class ArtifactWorkflow extends Error {
var $ath;
var $artifact_id;
var $field_id;
function __construct($artifact, $field_id) {
$this->ath = $artifact;
$this->artifact_id = (int)$artifact->getID();
$this->field_id = (int)$field_id;
}
// Check if the following event is allowed or not.
// return true is allowed, false if not.
function checkEvent($from, $to) {
if ($from === $to)
return true;
$res = db_query_params ('SELECT event_id FROM artifact_workflow_event
WHERE group_artifact_id=$1
AND field_id=$2
AND from_value_id=$3
AND to_value_id=$4',
array($this->artifact_id,
$this->field_id,
$from,
$to));
$event_id = db_result($res, 0, 'event_id');
if ($event_id) {
// No role based checks for the initial transition.
if ($from == 100)
return true;
// There is a transition, now check if current role is allowed.
$rids = array () ;
$available_roles = RBACEngine::getInstance()->getAvailableRoles() ;
$project_role_ids = $this->ath->Group->getRolesId () ;
foreach ($available_roles as $role) {
if (in_array($role->getID(),$project_role_ids)) {
$rids[] = $role->getID() ;
}
}
$res = db_query_params ('SELECT event_id
FROM artifact_workflow_roles
WHERE event_id=$1
AND role_id=ANY($2)',
array ($event_id,
db_int_array_to_any_clause($rids)));
return db_result($res, 0, 'event_id') ? true : false;
}
return false;
}
function getNotifyFromWorkFlow() {
}
/*
* When a new element is created, add all the new events in the workflow.
*/
function addNode($element_id) {
$elearray = $this->ath->getExtraFieldElements($this->field_id);
foreach ($elearray as $e) {
if ($element_id !== $e['element_id']) {
$this->_addEvent($e['element_id'], $element_id);
$this->_addEvent($element_id, $e['element_id']);
}
}
// Allow the new element for the Submit form (Initial values).
$this->_addEvent('100', $element_id);
}
/*
* When a new element is removed, remove all the events in the workflow.
*/
function removeNode($element_id) {
$elearray = $this->ath->getExtraFieldElements($this->field_id);
foreach ($elearray as $e) {
if ($element_id !== $e['element_id']) {
$this->_removeEvent($e['element_id'], $element_id);
$this->_removeEvent($element_id, $e['element_id']);
}
}
// Allow the new element for the Submit form (Initial values).
$this->_removeEvent('100', $element_id);
}
// Returns all the possible following nodes (no roles involved).
function getNextNodes($from) {
$res = db_query_params ('SELECT to_value_id FROM artifact_workflow_event
WHERE group_artifact_id=$1
AND field_id=$2
AND from_value_id=$3',
array($this->artifact_id,
$this->field_id,
(int)$from));
$values = array();
while($arr = db_fetch_array($res)) {
$values[] = $arr['to_value_id'];
}
return $values;
}
function saveNextNodes($from, $nodes) {
// Get All possible nodes.
$current = $this->getNextNodes($from);
// Remove events no longer present.
foreach ($current as $node) {
if (!in_array($node, $nodes)) {
if ($from != $node) {
$this->_removeEvent($from, $node);
}
}
}
// Add missing events.
foreach ($nodes as $node) {
if (!in_array($node, $current)) {
$this->_addEvent($from, $node);
}
}
return true;
}
function getAllowedRoles($from, $to) {
$values = $this->_getRealAllowedRoles($from, $to);
// If no values, then no roles defined, all roles are allowed.
if (empty($values)) {
$roles = $this->ath->Group->getRoles() ;
sortRoleList($roles, $this->ath->Group) ;
foreach ($roles as $r) {
$values[] = $r->getID() ;
}
}
return $values;
}
function saveAllowedRoles($from, $to, $roles) {
$event_id = $this->_getEventId($from, $to);
// Get All possible roles.
$current = $this->_getRealAllowedRoles($from, $to);
// Remove roles no longer present.
foreach ($current as $role) {
if (!in_array($role, $roles)) {
$this->_removeRole($event_id, $role);
}
}
// Add missing roles.
foreach ($roles as $role) {
if (!in_array($role, $current)) {
$this->_addRole($event_id, $role);
}
}
return true;
}
function _getEventId($from, $to) {
$res = db_query_params ('SELECT event_id FROM artifact_workflow_event
WHERE group_artifact_id=$1
AND field_id=$2
AND from_value_id=$3
AND to_value_id=$4',
array($this->artifact_id,
$this->field_id,
$from,
$to));
if (!$res) {
$this->setError('Unable to get Event Id ($from, $to): '.db_error());
return false;
}
return db_result($res, 0, 'event_id');
}
function _addEvent($from, $to) {
$res = db_query_params ('INSERT INTO artifact_workflow_event
(group_artifact_id, field_id, from_value_id, to_value_id)
VALUES ($1, $2, $3, $4)',
array($this->artifact_id,
$this->field_id,
$from,
$to));
if (!$res) {
$this->setError('Unable to add Event($from, $to): '.db_error());
return false;
}
$event_id = $this->_getEventId($from, $to);
if ($event_id) {
// By default, all roles are allowed on a new event.
foreach ($this->ath->Group->getRoles() as $r) {
$this->_addRole($event_id, $r->getID());
}
}
return true;
}
function _removeEvent($from, $to) {
$event_id = $this->_getEventId($from, $to);
$res = db_query_params ('DELETE FROM artifact_workflow_event
WHERE group_artifact_id=$1
AND field_id=$2
AND from_value_id=$3
AND to_value_id=$4',
array($this->artifact_id,
$this->field_id,
$from,
$to));
if (!$res) {
$this->setError('Unable to remove Event($from, $to): '.db_error());
return false;
}
return true;
}
function _getRealAllowedRoles($from, $to) {
$res = db_query_params ('SELECT role_id
FROM artifact_workflow_roles, artifact_workflow_event
WHERE artifact_workflow_roles.event_id = artifact_workflow_event.event_id
AND group_artifact_id=$1
AND field_id=$2
AND from_value_id=$3
AND to_value_id=$4',
array($this->artifact_id,
$this->field_id,
$from,
$to));
$values = array();
while($arr = db_fetch_array($res)) {
$values[] = $arr['role_id'];
}
return $values;
}
function _addRole($event_id, $role_id) {
$res = db_query_params ('INSERT INTO artifact_workflow_roles
(event_id, role_id)
VALUES ($1, $2)',
array($event_id,
$role_id));
if (!$res) {
$this->setError('Unable to add Role ($role_id): '.db_error());
return false;
}
return true;
}
function _removeRole($event_id, $role_id) {
$res = db_query_params ('DELETE FROM artifact_workflow_roles
WHERE event_id=$1 AND role_id=$2',
array($event_id,
$role_id));
if (!$res) {
$this->setError('Unable to remove Event($from, $to): '.db_error());
return false;
}
return true;
}
}
/*
* Update the required information in the workflow when a new role is created.
* In this case, for all the defined events, add the role as allowed.
*/
function workflow_add_new_role ($role_id, $group) {
$res = db_query_params ('INSERT INTO artifact_workflow_roles
SELECT event_id, $1 as role_id
FROM artifact_workflow_event, artifact_group_list
WHERE artifact_workflow_event.group_artifact_id=artifact_group_list.group_artifact_id
AND artifact_group_list.group_id=$2',
array($role_id,
$group->getID()));
if (!$res) {
$this->setError('Unable to register new role in workflows: '.db_error());
return false;
}
return true;
}
|