CommonDBTM::can PHP Method

can() public method

Check right on an item
public can ( $ID, $right, array &$input = NULL ) : boolean
$ID ID of the item (-1 if new item)
$right Right to check : r / w / recursive / READ / UPDATE / DELETE
$input array
return boolean
    function can($ID, $right, array &$input = NULL)
    {
        // Clean ID :
        $ID = Toolbox::cleanInteger($ID);
        // Create process
        if ($this->isNewID($ID)) {
            if (!isset($this->fields['id'])) {
                // Only once
                $this->getEmpty();
            }
            if (is_array($input)) {
                $input = $this->addNeededInfoToInput($input);
                // Copy input field to allow getEntityID() to work
                // from entites_id field or from parent item ref
                foreach ($input as $key => $val) {
                    if (isset($this->fields[$key])) {
                        $this->fields[$key] = $val;
                    }
                }
                // Store to be available for others functions
                $this->input = $input;
            }
            if ($this->isPrivate() && $this->fields['users_id'] === Session::getLoginUserID()) {
                return true;
            }
            return static::canCreate() && $this->canCreateItem();
        }
        // else : Get item if not already loaded
        if (!isset($this->fields['id']) || $this->fields['id'] != $ID) {
            // Item not found : no right
            if (!$this->getFromDB($ID)) {
                return false;
            }
        }
        switch ($right) {
            case READ:
                // Personnal item
                if ($this->isPrivate() && $this->fields['users_id'] === Session::getLoginUserID()) {
                    return true;
                }
                return static::canView() && $this->canViewItem();
            case UPDATE:
                // Personnal item
                if ($this->isPrivate() && $this->fields['users_id'] === Session::getLoginUserID()) {
                    return true;
                }
                return static::canUpdate() && $this->canUpdateItem();
            case DELETE:
                // Personnal item
                if ($this->isPrivate() && $this->fields['users_id'] === Session::getLoginUserID()) {
                    return true;
                }
                return static::canDelete() && $this->canDeleteItem();
            case PURGE:
                // Personnal item
                if ($this->isPrivate() && $this->fields['users_id'] === Session::getLoginUserID()) {
                    return true;
                }
                return static::canPurge() && $this->canPurgeItem();
            case CREATE:
                // Personnal item
                if ($this->isPrivate() && $this->fields['users_id'] === Session::getLoginUserID()) {
                    return true;
                }
                return static::canCreate() && $this->canCreateItem();
            case 'recursive':
                if ($this->isEntityAssign() && $this->maybeRecursive()) {
                    if (static::canCreate() && Session::haveAccessToEntity($this->getEntityID())) {
                        // Can make recursive if recursive access to entity
                        return Session::haveRecursiveAccessToEntity($this->getEntityID());
                    }
                }
                break;
        }
        return false;
    }

Usage Example

 /**
  * @since version 0.85
  *
  * @see CommonDBTM::processMassiveActionsForOneItemtype()
  **/
 static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids)
 {
     switch ($ma->getAction()) {
         case 'add':
             $input = $ma->getInput();
             $ticket = new Ticket();
             if (isset($input['link']) && isset($input['tickets_id_1'])) {
                 if ($item->getFromDB($input['tickets_id_1'])) {
                     foreach ($ids as $id) {
                         $input2 = array();
                         $input2['id'] = $input['tickets_id_1'];
                         $input2['_link']['tickets_id_1'] = $input['tickets_id_1'];
                         $input2['_link']['link'] = $input['link'];
                         $input2['_link']['tickets_id_2'] = $id;
                         if ($item->can($input['tickets_id_1'], UPDATE)) {
                             if ($ticket->update($input2)) {
                                 $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
                             } else {
                                 $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                                 $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
                             }
                         } else {
                             $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
                             $ma->addMessage($item->getErrorMessage(ERROR_RIGHT));
                         }
                     }
                 }
             }
             return;
     }
     parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
 }
All Usage Examples Of CommonDBTM::can
CommonDBTM