Ejemplo n.º 1
0
/**
 * Tells whether a user can assign a task to another user or company in a workspace.
 * 
 * @param $user User to which to check permissions
 * @param $workspace
 * @param $assignee
 * @return boolean
 */
function can_assign_task(User $user, Project $workspace, $assignee)
{
    if (!$assignee instanceof User && !$assignee instanceof Company) {
        return true;
    }
    if ($assignee instanceof Company) {
        $company = $assignee;
    } else {
        if ($assignee->getId() == $user->getId()) {
            return true;
        }
        // alow user to assign to himself
        $company = $assignee->getCompany();
    }
    $is_owner = $company->getId() == Companies::getOwnerCompany()->getId();
    $permissions = ProjectUsers::getByUserAndProject($workspace, $user);
    if ($permissions instanceof ProjectUser) {
        if ($is_owner) {
            if ($permissions->getCanAssignToOwners()) {
                return true;
            }
        } else {
            if ($permissions->getCanAssignToOther()) {
                return true;
            }
        }
    }
    $groups = GroupUsers::getGroupsByUser($user->getId());
    if (is_array($groups) && count($groups) > 0) {
        //user belongs to at least one group
        foreach ($groups as $group) {
            $permissions = ProjectUsers::getByUserAndProject($workspace, $group);
            if ($permissions instanceof ProjectUser) {
                if ($is_owner) {
                    if ($permissions->getCanAssignToOwners()) {
                        return true;
                    }
                } else {
                    if ($permissions->getCanAssignToOther()) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
 /**
  * Edit group
  *
  * @param void
  * @return null
  */
 function edit_group()
 {
     $this->setTemplate('add_group');
     if (!can_manage_security(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     // if
     $group = Groups::findById(get_id());
     if (!$group instanceof Group) {
         flash_error(lang('group dnx'));
         $this->redirectTo('administration', 'groups');
     }
     // if
     if (logged_user()->isAdministrator()) {
         $projects = Projects::getAll();
     } else {
         $projects = null;
     }
     $permissions = ProjectUsers::getNameTextArray();
     $group_data = array_var($_POST, 'group');
     if (!is_array($group_data)) {
         $group_data = array('name' => $group->getName(), 'can_edit_company_data' => $group->getCanEditCompanyData(), 'can_manage_security' => $group->getCanManageSecurity(), 'can_manage_workspaces' => $group->getCanManageWorkspaces(), 'can_manage_configuration' => $group->getCanManageConfiguration(), 'can_manage_contacts' => $group->getCanManageContacts(), 'can_manage_templates' => $group->getCanManageTemplates(), 'can_manage_reports' => $group->getCanManageReports(), 'can_manage_time' => $group->getCanManageTime(), 'can_add_mail_accounts' => $group->getCanAddMailAccounts());
         // array
     }
     // if
     $users = GroupUsers::getUsersByGroup($group->getId());
     if ($users) {
         foreach ($users as $usr) {
             $group_data['user[' . $usr->getId() . ']'] = true;
         }
     }
     tpl_assign('group', $group);
     tpl_assign('group_data', $group_data);
     tpl_assign('permissions', $permissions);
     tpl_assign('projects', $projects);
     if (is_array(array_var($_POST, 'group'))) {
         $group->setFromAttributes($group_data);
         if (array_var($group_data, "can_edit_company_data") != 'checked') {
             $group->setCanEditCompanyData(false);
         }
         if (array_var($group_data, "can_manage_security") != 'checked') {
             $group->setCanManageSecurity(false);
         }
         if (array_var($group_data, "can_manage_configuration") != 'checked') {
             $group->setCanManageConfiguration(false);
         }
         if (array_var($group_data, "can_manage_workspaces") != 'checked') {
             $group->setCanManageWorkspaces(false);
         }
         if (array_var($group_data, "can_manage_contacts") != 'checked') {
             $group->setCanManageContacts(false);
         }
         if (array_var($group_data, "can_manage_templates") != 'checked') {
             $group->setCanManageTemplates(false);
         }
         if (array_var($group_data, "can_manage_reports") != 'checked') {
             $group->setCanManageReports(false);
         }
         if (array_var($group_data, "can_manage_time") != 'checked') {
             $group->setCanManageTime(false);
         }
         if (array_var($group_data, "can_add_mail_accounts") != 'checked') {
             $group->setCanAddMailAccounts(false);
         }
         try {
             DB::beginWork();
             //set permissions
             $permissionsString = array_var($_POST, 'permissions');
             if ($permissionsString && $permissionsString != '') {
                 $permissions = json_decode($permissionsString);
             }
             if (is_array($permissions) && count($permissions) > 0) {
                 //Clear old modified permissions
                 $ids = array();
                 foreach ($permissions as $perm) {
                     $ids[] = $perm->wsid;
                 }
                 ProjectUsers::clearByUser($group, implode(',', $ids));
                 //Add new permissions
                 //TODO - Make batch update of these permissions
                 foreach ($permissions as $perm) {
                     if (ProjectUser::hasAnyPermissions($perm->pr, $perm->pc)) {
                         $relation = new ProjectUser();
                         $relation->setProjectId($perm->wsid);
                         $relation->setUserId($group->getId());
                         $relation->setCheckboxPermissions($perm->pc);
                         $relation->setRadioPermissions($perm->pr);
                         $relation->save();
                     }
                     //endif
                     //else if the user has no permissions at all, he is not a project_user. ProjectUser is not created
                 }
                 //end foreach
             }
             // if
             $group->save();
             GroupUsers::clearByGroup($group);
             if (array_var($_POST, 'user')) {
                 foreach (array_var($_POST, 'user') as $user_id => $val) {
                     if ($val == 'checked' && is_numeric($user_id) && Users::findById($user_id) instanceof User) {
                         $gu = new GroupUser();
                         $gu->setGroupId($group->getId());
                         $gu->setUserId($user_id);
                         $gu->save();
                     }
                 }
             }
             ApplicationLogs::createLog($group, null, ApplicationLogs::ACTION_EDIT);
             DB::commit();
             flash_success(lang('success edit group', $group->getName()));
             ajx_current("back");
         } catch (Exception $e) {
             DB::rollback();
             tpl_assign('error', $e);
         }
         // try
     }
     // if
 }
Ejemplo n.º 3
0
 function getProjectIdsByUser(User $user, $additional_conditions = null, $order_by = null)
 {
     $projects_table = Projects::instance()->getTableName(true);
     $project_users_table = ProjectUsers::instance()->getTableName(true);
     $group_users_table = GroupUsers::instance()->getTableName(true);
     $projects = array();
     $usercond = "({$project_users_table}.`user_id` = " . DB::escape($user->getId()) . ")";
     $groupcond = "({$project_users_table}.`user_id` IN (SELECT `group_id` FROM {$group_users_table} WHERE {$group_users_table}.`user_id` = " . DB::escape($user->getId()) . "))";
     $commoncond = "{$projects_table}.`id` = {$project_users_table}.`project_id`";
     $sql = "SELECT {$projects_table}.`id` as `id` FROM {$projects_table}, {$project_users_table} WHERE {$commoncond} AND ({$usercond} OR {$groupcond}) ";
     if (trim($additional_conditions) != '') {
         $sql .= " AND ({$additional_conditions})";
     }
     // if
     if ($order_by) {
         $sql .= " ORDER BY '" . $order_by;
     } else {
         $sql .= " ORDER BY {$projects_table}.`name`";
     }
     $rows = DB::executeAll($sql);
     $ids = array();
     foreach ($rows as $row) {
         $ids[] = $row['id'];
     }
     return $ids;
 }
Ejemplo n.º 4
0
 /**
  * Return manager instance
  *
  * @access protected
  * @param void
  * @return GroupUser 
  */
 function manager()
 {
     if (!$this->manager instanceof GroupUsers) {
         $this->manager = GroupUsers::instance();
     }
     return $this->manager;
 }
Ejemplo n.º 5
0
 /**
  * Return number of group users
  *
  * @access public
  * @param void
  * @return integer
  */
 function countUsers()
 {
     return GroupUsers::count('`group_id` = ' . DB::escape($this->getId()));
 }
Ejemplo n.º 6
0
 /**
  * Delete this object
  *
  * @param void
  * @return boolean
  */
 function delete()
 {
     if ($this->isAccountOwner()) {
         return false;
     }
     // if
     $this->deleteAvatar();
     //$this->deletePersonalProject();
     MailAccountUsers::deleteByUser($this);
     GroupUsers::clearByUser($this);
     Contacts::updateUserIdOnUserDelete($this->getId());
     ProjectUsers::clearByUser($this);
     ObjectSubscriptions::clearByUser($this);
     ObjectReminders::clearByUser($this);
     EventInvitations::clearByUser($this);
     UserPasswords::clearByUser($this);
     return parent::delete();
 }
 /**
  * This function will return paginated result. Result is an array where first element is 
  * array of returned object and second populated pagination object that can be used for 
  * obtaining and rendering pagination data using various helpers.
  * 
  * Items and pagination array vars are indexed with 0 for items and 1 for pagination
  * because you can't use associative indexing with list() construct
  *
  * @access public
  * @param array $arguments Query argumens (@see find()) Limit and offset are ignored!
  * @param integer $items_per_page Number of items per page
  * @param integer $current_page Current page number
  * @return array
  */
 function paginate($arguments = null, $items_per_page = 10, $current_page = 1)
 {
     if (isset($this) && instance_of($this, 'GroupUsers')) {
         return parent::paginate($arguments, $items_per_page, $current_page);
     } else {
         return GroupUsers::instance()->paginate($arguments, $items_per_page, $current_page);
         //$instance =&  GroupUsers::instance();
         //return $instance->paginate($arguments, $items_per_page, $current_page);
     }
     // if
 }