/** * Edit project * * @param void * @return null */ function edit() { if (logged_user()->isGuest()) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } $this->setTemplate('add_project'); $project = Projects::findById(get_id()); if (!$project instanceof Project) { flash_error(lang('project dnx')); ajx_current("empty"); return; } // if if (!$project->canEdit(logged_user())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } // if $project_data = array_var($_POST, 'project'); if (!is_array($project_data)) { $project_data = array('name' => $project->getName(), 'description' => $project->getDescription(), 'show_description_in_overview' => $project->getShowDescriptionInOverview(), 'color' => 0); // array } // if $projects = logged_user()->getActiveProjects(); tpl_assign('project', $project); tpl_assign('projects', $projects); tpl_assign('project_data', $project_data); tpl_assign('billing_amounts', $project->getBillingAmounts()); tpl_assign('subject_matter_experts', ProjectContacts::getContactsByProject($project)); if (is_array(array_var($_POST, 'project'))) { if (array_var($project_data, 'parent_id') == $project->getId()) { flash_error(lang("workspace own parent error")); ajx_current("empty"); return; } if (!isset($project_data['parent_id'])) { $project_data['parent_id'] = $project->getParentId(); } $project->setFromAttributes($project_data); try { DB::beginWork(); if (array_var($project_data, 'parent_id') != $project->getParentId()) { if ($project->getParentWorkspace() instanceof Project && !logged_user()->isProjectUser($project->getParentWorkspace())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } // if $parent = Projects::findById(array_var($project_data, 'parent_id')); if ($parent) { if (!$project->canSetAsParentWorkspace($parent)) { flash_error(lang('error cannot set workspace as parent', $parent->getName())); ajx_current("empty"); return; } } $project->setParentWorkspace($parent); } $project->save(); /* Billing */ WorkspaceBillings::clearByProject($project); $billings = array_var($project_data, 'billing', null); if ($billings) { foreach ($billings as $billing_id => $billing) { if ($billing['update'] && $billing['value'] && $billing['value'] != 0) { $wb = new WorkspaceBilling(); $wb->setProjectId($project->getId()); $wb->setBillingId($billing_id); $value = $billing['value']; if (strpos($value, ',') && !strpos($value, '.')) { $value = str_replace(',', '.', $value); } $wb->setValue($value); $wb->save(); } } } /* Project contacts */ if (can_manage_contacts(logged_user())) { ProjectContacts::clearByProject($project); $contacts = array_var($project_data, 'contacts', null); if ($contacts) { foreach ($contacts as $contact_data) { $contact = Contacts::findById($contact_data['contact_id']); if ($contact instanceof Contact) { $pc = new ProjectContact(); $pc->setProjectId($project->getId()); $pc->setContactId($contact_data['contact_id']); $pc->setRole($contact_data['role']); $pc->save(); } } } } /* <permissions> */ $permissions = null; $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::clearByProject($project, 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($project->getId()); $relation->setUserId($perm->wsid); $relation->setCheckboxPermissions($perm->pc, $relation->getUserOrGroup()->isGuest() ? false : true); $relation->setRadioPermissions($perm->pr, $relation->getUserOrGroup()->isGuest() ? false : true); $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 /* </permissions> */ $object_controller = new ObjectController(); $object_controller->add_custom_properties($project); ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_EDIT, false, true); DB::commit(); if (logged_user()->isProjectUser($project)) { $workspace_info = $this->get_workspace_info($project); evt_add("workspace edited", $workspace_info); } flash_success(lang('success edit project', $project->getName())); ajx_current("back"); return; } catch (Exception $e) { DB::rollback(); flash_error($e->getMessage()); ajx_current("empty"); } // try } // if }
function create_user($user_data, $permissionsString) { $user = new User(); $user->setUsername(array_var($user_data, 'username')); $user->setDisplayName(array_var($user_data, 'display_name')); $user->setEmail(array_var($user_data, 'email')); $user->setCompanyId(array_var($user_data, 'company_id')); $user->setType(array_var($user_data, 'type')); $user->setTimezone(array_var($user_data, 'timezone')); if (!logged_user() instanceof User || can_manage_security(logged_user())) { $user->setCanEditCompanyData(array_var($user_data, 'can_edit_company_data')); $user->setCanManageSecurity(array_var($user_data, 'can_manage_security')); $user->setCanManageWorkspaces(array_var($user_data, 'can_manage_workspaces')); $user->setCanManageConfiguration(array_var($user_data, 'can_manage_configuration')); $user->setCanManageContacts(array_var($user_data, 'can_manage_contacts')); $user->setCanManageTemplates(array_var($user_data, 'can_manage_templates')); $user->setCanManageReports(array_var($user_data, 'can_manage_reports')); $user->setCanManageTime(array_var($user_data, 'can_manage_time')); $user->setCanAddMailAccounts(array_var($user_data, 'can_add_mail_accounts')); $other_permissions = array(); Hook::fire('add_user_permissions', $user, $other_permissions); foreach ($other_permissions as $k => $v) { $user->setColumnValue($k, array_var($user_data, $k)); } } if (array_var($user_data, 'password_generator', 'random') == 'random') { // Generate random password $password = UserPasswords::generateRandomPassword(); } else { // Validate input $password = array_var($user_data, 'password'); if (trim($password) == '') { throw new Error(lang('password value required')); } // if if ($password != array_var($user_data, 'password_a')) { throw new Error(lang('passwords dont match')); } // if } // if $user->setPassword($password); $user->save(); $user_password = new UserPassword(); $user_password->setUserId($user->getId()); $user_password->setPasswordDate(DateTimeValueLib::now()); $user_password->setPassword(cp_encrypt($password, $user_password->getPasswordDate()->getTimestamp())); $user_password->password_temp = $password; $user_password->save(); if (array_var($user_data, 'autodetect_time_zone', 1) == 1) { set_user_config_option('autodetect_time_zone', 1, $user->getId()); } if ($user->getType() == 'admin') { if ($user->getCompanyId() != owner_company()->getId() || logged_user() instanceof User && !can_manage_security(logged_user())) { // external users can't be admins or logged user has no rights to create admins => set as Normal $user->setType('normal'); } else { $user->setAsAdministrator(true); } } /* create contact for this user*/ if (array_var($user_data, 'create_contact', 1)) { // if contact with same email exists take it, else create new $contact = Contacts::getByEmail($user->getEmail(), true); if (!$contact instanceof Contact) { $contact = new Contact(); $contact->setEmail($user->getEmail()); } else { if ($contact->isTrashed()) { $contact->untrash(); } } $contact->setFirstname($user->getDisplayName()); $contact->setUserId($user->getId()); $contact->setTimezone($user->getTimezone()); $contact->setCompanyId($user->getCompanyId()); $contact->save(); } else { $contact_id = array_var($user_data, 'contact_id'); $contact = Contacts::findById($contact_id); if ($contact instanceof Contact) { // user created from a contact $contact->setUserId($user->getId()); $contact->save(); } else { // if contact with same email exists use it as user's contact, without changing it $contact = Contacts::getByEmail($user->getEmail(), true); if ($contact instanceof Contact) { $contact->setUserId($user->getId()); if ($contact->isTrashed()) { $contact->untrash(); } $contact->save(); } } } $contact = $user->getContact(); if ($contact instanceof Contact) { // update contact data with data entered for this user $contact->setCompanyId($user->getCompanyId()); if ($contact->getEmail() != $user->getEmail()) { // make user's email the contact's main email address if ($contact->getEmail2() == $user->getEmail()) { $contact->setEmail2($contact->getEmail()); } else { if ($contact->getEmail3() == $user->getEmail()) { $contact->setEmail3($contact->getEmail()); } else { if ($contact->getEmail2() == "") { $contact->setEmail2($contact->getEmail()); } else { $contact->setEmail3($contact->getEmail()); } } } } $contact->setEmail($user->getEmail()); $contact->save(); } if (!$user->isGuest()) { /* create personal project or assing the selected*/ //if recived a personal project assing this //project as personal project for this user $new_project = null; $personalProjectId = array_var($user_data, 'personal_project', 0); $project = Projects::findById($personalProjectId); if (!$project instanceof Project) { $project = new Project(); $wname = new_personal_project_name($user->getUsername()); $project->setName($wname); $wdesc = Localization::instance()->lang(lang('personal workspace description')); if (!is_null($wdesc)) { $project->setDescription($wdesc); } $project->setCreatedById($user->getId()); $project->save(); //Save to set an ID number $project->setP1($project->getId()); //Set ID number to the first project $project->save(); $new_project = $project; } $user->setPersonalProjectId($project->getId()); $project_user = new ProjectUser(); $project_user->setProjectId($project->getId()); $project_user->setUserId($user->getId()); $project_user->setCreatedById($user->getId()); $project_user->setAllPermissions(true); $project_user->save(); /* end personal project */ } $user->save(); ApplicationLogs::createLog($user, null, ApplicationLogs::ACTION_ADD); //TODO - Make batch update of these permissions if ($permissionsString && $permissionsString != '') { $permissions = json_decode($permissionsString); } else { $permissions = null; } if (is_array($permissions) && (!logged_user() instanceof User || can_manage_security(logged_user()))) { foreach ($permissions as $perm) { if (ProjectUser::hasAnyPermissions($perm->pr, $perm->pc)) { if (!$personalProjectId || $personalProjectId != $perm->wsid) { $relation = new ProjectUser(); $relation->setProjectId($perm->wsid); $relation->setUserId($user->getId()); $relation->setCheckboxPermissions($perm->pc, $user->isGuest() ? false : true); $relation->setRadioPermissions($perm->pr, $user->isGuest() ? false : true); $relation->save(); } } } } // if if ($new_project instanceof Project && logged_user() instanceof User && logged_user()->isProjectUser($new_project)) { evt_add("workspace added", array("id" => $new_project->getId(), "name" => $new_project->getName(), "color" => $new_project->getColor())); } // Send notification... try { if (array_var($user_data, 'send_email_notification')) { Notifier::newUserAccount($user, $password); } // if } catch (Exception $e) { } // try return $user; }
/** * 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 }
/** * Show update permissions page * * @param void * @return null */ function update_permissions() { $user = Users::findById(get_id()); if (!$user instanceof User) { flash_error(lang('user dnx')); ajx_current("empty"); return; } // if if (!$user->canUpdatePermissions(logged_user())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } // if $company = $user->getCompany(); if (!$company instanceof Company) { flash_error(lang('company dnx')); ajx_current("empty"); return; } // if if (logged_user()->isAdministrator()) { $projects = Projects::getAll(); } else { $projects = null; } $permissions = ProjectUsers::getNameTextArray(); $redirect_to = array_var($_GET, 'redirect_to'); if (trim($redirect_to) == '' || !is_valid_url($redirect_to)) { $redirect_to = $user->getCardUrl(); } // if $user_data = array_var($_POST, 'user'); if (!is_array($user_data)) { $user_data = array('can_edit_company_data' => $user->getCanEditCompanyData(), 'can_manage_security' => $user->getCanManageSecurity(), 'can_manage_workspaces' => $user->getCanManageWorkspaces(), 'can_manage_configuration' => $user->getCanManageConfiguration(), 'can_manage_contacts' => $user->getCanManageContacts(), 'can_manage_templates' => $user->getCanManageTemplates(), 'can_manage_reports' => $user->getCanManageReports(), 'can_manage_time' => $user->getCanManageTime(), 'can_add_mail_accounts' => $user->getCanAddMailAccounts()); // array Hook::fire('add_user_permissions', $user, $user_data); } // if tpl_assign('user_data', $user_data); tpl_assign('user', $user); tpl_assign('company', $company); tpl_assign('projects', $projects); tpl_assign('permissions', $permissions); tpl_assign('redirect_to', $redirect_to); if (array_var($_POST, 'submitted') == 'submitted') { $user_data = array_var($_POST, 'user'); if (!is_array($user_data)) { $user_data = array(); } try { DB::beginWork(); $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($user, 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($user->getId()); $relation->setCheckboxPermissions($perm->pc, $user->isGuest() ? false : true); $relation->setRadioPermissions($perm->pr, $user->isGuest() ? false : true); $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 $user->setCanEditCompanyData(false); $user->setCanManageSecurity(false); $user->setCanManageConfiguration(false); $user->setCanManageWorkspaces(false); $user->setCanManageContacts(false); $user->setCanManageTemplates(false); $user->setCanManageReports(false); $user->setCanManageTime(false); $user->setCanAddMailAccounts(false); $other_permissions = array(); Hook::fire('add_user_permissions', $user, $other_permissions); foreach ($other_permissions as $k => $v) { $user->setColumnValue($k, false); } $user->setFromAttributes($user_data); $user->setUpdatedOn(DateTimeValueLib::now()); $user->save(); DB::commit(); flash_success(lang('success user permissions updated')); ajx_current("back"); } catch (Exception $e) { DB::rollback(); flash_error($e->getMessage()); ajx_current("empty"); } } // if }