Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Use company profile module
- use_controller('company_profile', SYSTEM_MODULE);
- /**
- * User profile controller
- *
- * @package activeCollab.modules.system
- * @subpackage controllers
- */
- class UserProfileController extends CompanyProfileController {
- /**
- * Name of this controller
- *
- * @var string
- */
- var $controller_name = 'user_profile';
- /**
- * Name of the parent module
- *
- * @var mixed
- */
- var $active_module = SYSTEM_MODULE;
- /**
- * Selected use
- *
- * @var User
- */
- var $active_user;
- /**
- * Array of controller actions that can be accessed through API
- *
- * @var array
- */
- var $api_actions = array('index', 'add', 'edit', 'delete');
- /**
- * Construct Profile Controller
- *
- * @param void
- * @return null
- */
- function __construct($request){
- parent::__construct($request);
- $user_id = $this->request->get('user_id');
- if($user_id) {
- $this->active_user = Users::findById($user_id);
- } // if
- if(instance_of($this->active_user,'User')) {
- if(!in_array($this->active_user->getId(), $this->logged_user->visibleUserIds())) {
- $this->httpError(HTTP_ERR_NOT_FOUND);
- } // if
- $this->wireframe->addBreadCrumb($this->active_user->getName(), $this->active_user->getViewUrl());
- } else {
- $this->active_user = new User();
- } // if
- $this->smarty->assign('active_user', $this->active_user);
- } // __construct
- /**
- * Show user profile page
- *
- * @param void
- * @return null
- */
- function index() {
- if($this->active_user->isNew()) {
- $this->httpError(HTTP_ERR_NOT_FOUND, null, true, $this->request->isApiCall());
- } // if
- if($this->request->isApiCall()) {
- $this->serveData($this->active_user, 'user', array(
- 'describe_company' => true,
- 'describe_avatar' => true,
- ));
- } else {
- $this->redirectToUrl($this->active_company->getViewUrl() . '#user' . $this->active_user->getId());
- } // if
- } // index
- /**
- * Create new user
- *
- * @param void
- * @return null
- */
- function add() {
- $this->wireframe->print_button = false;
- if($this->request->isApiCall() && !$this->request->isSubmitted()) {
- $this->httpError(HTTP_ERR_BAD_REQUEST);
- } // if
- if(!User::canAdd($this->logged_user, $this->active_company)) {
- $this->httpError(HTTP_ERR_FORBIDDEN);
- } // if
- $user_data = $this->request->post('user');
- if(!is_array($user_data)) {
- $user_data = array(
- 'role_id' => ConfigOptions::getValue('default_role'),
- 'auto_assign' => false,
- );
- } // if
- $this->smarty->assign(array(
- 'user_data' => $user_data,
- ));
- if($this->request->isSubmitted()) {
- db_begin_work();
- // Validate password
- if(array_var($user_data, 'password_generator') == 'generate') {
- $password = make_password(11);
- } else {
- $errors = new ValidationErrors();
- $password = array_var($user_data, 'password');
- $password_a = array_var($user_data, 'password_a');
- if(strlen(trim($password)) < 3) {
- $errors->addError(lang('Minimal password length is 3 characters'), 'password');
- } else {
- if($password != $password_a) {
- $errors->addError(lang('Passwords do not match'), 'passwords');
- } // if
- } // if
- if($errors->hasErrors()) {
- if($this->request->getFormat() == FORMAT_HTML) {
- $this->smarty->assign('errors', $errors);
- $this->render();
- } else {
- $this->serveData($errors);
- } // if
- } // if
- } // if
- $this->active_user = new User();
- $this->active_user->setAttributes($user_data);
- $this->active_user->setPassword($password);
- $this->active_user->setCompanyId($this->active_company->getId());
- if($this->logged_user->isPeopleManager()) {
- $this->active_user->setAutoAssignData(
- (boolean) array_var($user_data, 'auto_assign'),
- (integer) array_var($user_data, 'auto_assign_role_id'),
- array_var($user_data, 'auto_assign_permissions')
- );
- } else {
- $this->active_user->setRoleId(ConfigOptions::getValue('default_role'));
- } // if
- $save = $this->active_user->save();
- if($save && !is_error($save)) {
- if(array_key_exists('send_welcome_message', $user_data) && $user_data['send_welcome_message']) {
- ApplicationMailer::send(array($this->active_user), 'system/new_user', array(
- 'created_by_id' => $this->logged_user->getId(),
- 'created_by_name' => $this->logged_user->getDisplayName(),
- 'created_by_url' => $this->logged_user->getViewUrl(),
- 'email' => $this->active_user->getEmail(),
- 'password' => $password,
- 'login_url' => assemble_url('login'),
- 'welcome_body' => nl2br(clean(array_var($user_data, 'welcome_message'))),
- ));
- } // if
- db_commit();
- if($this->request->isApiCall()) {
- $this->serveData($this->active_user);
- } else {
- flash_success('New user account has been created');
- $this->redirectToUrl($this->active_user->getViewUrl());
- } // if
- } else {
- db_rollback();
- if($this->request->isApiCall()) {
- $this->serveData($save);
- } else {
- $this->smarty->assign('errors', $save);
- } // if
- } // if
- } // if
- } // add
- /**
- * Edit Profile
- *
- * @param void
- * @return null
- */
- function edit() {
- $this->wireframe->print_button = false;
- if($this->active_user->isNew()) {
- $this->httpError(HTTP_ERR_NOT_FOUND);
- } // if
- if(!$this->active_user->canEdit($this->logged_user)) {
- $this->httpError(HTTP_ERR_FORBIDDEN);
- } // if
- $config_options = array('title', 'phone_work', 'phone_mobile', 'format_date', 'format_time', 'time_timezone', 'time_dst', 'time_first_week_day', 'visual_editor', 'im_type', 'im_value', 'theme');
- if(LOCALIZATION_ENABLED) {
- $config_options[] = 'language';
- } // if
- $user_data = $this->request->post('user');
- if(!is_array($user_data)) {
- $user_data = array_merge(array(
- 'company_id' => $this->active_user->getCompanyId(),
- 'first_name' => $this->active_user->getFirstName(),
- 'last_name' => $this->active_user->getLastName(),
- 'email' => $this->active_user->getEmail(),
- 'role_id' => $this->active_user->getRoleId(),
- 'auto_assign' => $this->active_user->getAutoAssign(),
- 'auto_assign_role_id' => $this->active_user->getAutoAssignRoleId(),
- 'auto_assign_permissions' => $this->active_user->getAutoAssignPermissions(),
- ), UserConfigOptions::getValues($config_options, $this->active_user));
- if(LOCALIZATION_ENABLED) {
- if(!UserConfigOptions::hasValue('language', $this->active_user)) {
- $user_data['language'] = null;
- } // if
- } // if
- if(!UserConfigOptions::hasValue('format_date', $this->active_user)) {
- $user_data['format_date'] = null;
- } // if
- if(!UserConfigOptions::hasValue('format_time', $this->active_user)) {
- $user_data['format_time'] = null;
- } // if
- } // if
- $this->smarty->assign(array(
- 'user_data' => $user_data,
- 'only_administrator' => $this->active_user->isOnlyAdministrator(),
- ));
- if($this->request->isSubmitted()) {
- db_begin_work();
- $display = $this->active_user->getDisplayName();
- $old_role_id = $this->active_user->getRoleId();
- if($this->active_user->isOnlyAdministrator()) {
- $user_data['role_id'] = $old_role_id; // cannot change role
- } // if
- $this->active_user->setAttributes($user_data);
- if($this->active_user->canChangeRole($this->logged_user)) {
- $this->active_user->setAutoAssignData(
- (boolean) array_var($user_data, 'auto_assign'),
- (integer) array_var($user_data, 'auto_assign_role_id'),
- array_var($user_data, 'auto_assign_permissions')
- );
- } else {
- $this->active_user->setRoleId($old_role_id);
- } // if
- $save = $this->active_user->save();
- if($save && !is_error($save)) {
- foreach($config_options as $config_option) {
- if($config_option == 'time_dst' || $config_option == 'visual_editor') {
- $value = (boolean) array_var($user_data, $config_option);
- } elseif($config_option == 'time_timezone' || $config_option == 'time_first_week_day ') {
- $value = (integer) array_var($user_data, $config_option);
- } else {
- $value = trim(array_var($user_data, $config_option));
- } // if
- if($value === '') {
- UserConfigOptions::removeValue($config_option, $this->active_user);
- } else {
- UserConfigOptions::setValue($config_option, $value, $this->active_user);
- } // if
- } // foreach
- db_commit();
- if($this->request->isApiCall()) {
- $this->serveData($this->active_user, 'user');
- } else {
- flash_success(":display's profile has been updated", array('display' => $display));
- $this->redirectToUrl($this->active_user->getViewUrl());
- } // if
- } else {
- db_rollback();
- if($this->request->isApiCall()) {
- $this->serveData($save);
- } else {
- $this->smarty->assign('errors', $save);
- } // if
- } // if
- } else {
- if($this->request->isApiCall()) {
- $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, true);
- } // if
- } // if
- } // edit
- /**
- * Edit Profile Password
- *
- * @param void
- * @return null
- */
- function edit_password() {
- $this->wireframe->print_button = false;
- if($this->active_user->isNew()) {
- $this->httpError(HTTP_ERR_NOT_FOUND);
- } // if
- if(!$this->active_user->canEdit($this->logged_user)) {
- $this->httpError(HTTP_ERR_FORBIDDEN);
- } // if
- $user_data = $this->request->post('user');
- $this->smarty->assign('user_data', $user_data);
- if($this->request->isSubmitted()) {
- $errors = new ValidationErrors();
- $password = array_var($user_data, 'password');
- $repeat_password = array_var($user_data, 'repeat_password');
- if(empty($password)) {
- $errors->addError(lang('Password value is required'), 'password');
- } // if
- if(strlen($password) < 8) {
- $errors->addError(lang('Passwords must be at least 8 characters'), 'password');
- } elseif(!preg_match("/(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/", $password)) {
- $errors->addError(lang('Please use at least one upper case and lower case character, along with a digit or special character'), 'password');
- }
- if(empty($repeat_password)) {
- $errors->addError(lang('Repeat Password value is required'), 'repeat_password');
- } // if
- if(!$errors->hasErrors() && ($password !== $repeat_password)) {
- $errors->addError(lang('Inserted values does not match'));
- } // if
- if($errors->hasErrors()) {
- $this->smarty->assign('errors', $errors);
- $this->render();
- } // if
- db_begin_work();
- $this->active_user->setPassword($user_data['password']);
- $save = $this->active_user->save();
- if($save && !is_error($save)) {
- db_commit();
- if($this->active_user->getNewAcc() == '0') {
- $this->active_user->setNewAcc('1');
- $this->active_user->save();
- }
- if($this->request->getFormat() == FORMAT_HTML) {
- flash_success('Password has been updated');
- $this->redirectToUrl($this->active_user->getViewUrl());
- } else {
- $this->serveData($this->active_user, 'user');
- } // if
- } else {
- db_rollback();
- if($this->request->getFormat() == FORMAT_HTML) {
- $this->smarty->assign('errors', $errors);
- } else {
- $this->serveData($errors);
- } // if
- } // if
- } // if
- } // edit_password
- /**
- * Edit Profile Avatar
- *
- * @param void
- * @return null
- */
- function edit_avatar() {
- $this->wireframe->print_button = false;
- if($this->active_user->isNew()) {
- $this->httpError(HTTP_ERR_NOT_FOUND);
- } // if
- if(!$this->active_user->canEdit($this->logged_user)) {
- $this->httpError(HTTP_ERR_FORBIDDEN);
- } // if
- if(!extension_loaded('gd')) {
- $this->wireframe->addPageMessage(lang('<b>GD not Installed</b> - GD extension is not installed on your system. You will not be able to upload project icons, company logos and avatars!'), PAGE_MESSAGE_ERROR);
- } // if
- if($this->request->isSubmitted()) {
- if(!isset($_FILES['avatar']) || !is_file($_FILES['avatar']['tmp_name'])) {
- flash_error("Please select an image");
- $this->redirectToUrl($this->active_user->getEditAvatarUrl());
- } // if
- if(can_resize_images()) {
- $errors = new ValidationErrors();
- do {
- $from = WORK_PATH.'/'.make_password(10).'_'.$_FILES['avatar']['name'];
- } while (is_file($from));
- if(move_uploaded_file($_FILES['avatar']['tmp_name'], $from)) {
- $to = $this->active_user->getAvatarPath();
- $small = scale_image($from, $to, 16, 16, IMAGETYPE_JPEG,100);
- $to = $this->active_user->getAvatarPath(true);
- $large = scale_image($from, $to, 40, 40, IMAGETYPE_JPEG,100);
- @unlink($from);
- } else {
- $errors->addError('Can\'t copy image to work path', 'icon');
- } // if
- if(empty($from)) {
- $errors->addError('Select avatar', 'avatar');
- } // if
- if($errors->hasErrors()) {
- $this->smarty->assign('errors', $errors);
- $this->render();
- } // if
- } // if
- } // if
- } // edit_avatar
- /**
- * Delete user
- *
- * @param void
- * @return null
- */
- function delete() {
- if($this->active_user->isNew()) {
- $this->httpError(HTTP_ERR_NOT_FOUND, null, true, $this->request->isApiCall());
- } // if
- if(!$this->active_user->canDelete($this->logged_user)) {
- $this->httpError(HTTP_ERR_FORBIDDEN, null, true, $this->request->isApiCall());
- } // if
- if($this->request->isSubmitted()) {
- $delete = $this->active_user->delete();
- if($delete && !is_error($delete)) {
- if($this->request->isApiCall()) {
- $this->httpOk();
- } else {
- flash_success('User ":name" has been deleted', array('name' => $this->active_user->getDisplayName()));
- $this->redirectToUrl($this->active_company->getViewUrl());
- } // if
- } else {
- if($this->request->isApiCall()) {
- $this->serveData($delete);
- } else {
- flash_error('Failed to delete ":name"', array('name' => $this->active_user->getDisplayName()));
- $this->redirectToUrl($this->active_company->getViewUrl());
- } // if
- } // if
- } else {
- $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, $this->request->isApiCall());
- } // if
- } // delete
- /**
- * Delete Profile Avatar
- *
- * @param void
- * @return null
- */
- function delete_avatar() {
- if($this->active_user->isNew()) {
- $this->httpError(HTTP_ERR_NOT_FOUND);
- } // if
- if(!$this->active_user->canEdit($this->logged_user)) {
- $this->httpError(HTTP_ERR_FORBIDDEN);
- } // if
- if($this->request->isSubmitted()) {
- unlink($this->active_user->getAvatarPath());
- unlink($this->active_user->getAvatarPath(true));
- $this->redirectToUrl($this->active_user->getEditAvatarUrl());
- } else {
- $this->httpError(HTTP_ERR_BAD_REQUEST);
- } // if
- } // delete_avatar
- /**
- * Show API settings URL
- *
- * @param void
- * @return null
- */
- function api() {
- if(!$this->active_user->canEdit($this->logged_user)) {
- $this->httpError(HTTP_ERR_FORBIDDEN);
- } // if
- $this->wireframe->print_button = false;
- $this->smarty->assign('api_url', ROOT_URL);
- } // api
- /**
- * Reset API key
- *
- * @param void
- * @return null
- */
- function api_reset_key() {
- if($this->active_user->isNew()) {
- $this->httpError(HTTP_ERR_NOT_FOUND);
- } // if
- if(!$this->active_user->canEdit($this->logged_user)) {
- $this->httpError(HTTP_ERR_FORBIDDEN);
- } // if
- if($this->request->isSubmitted()) {
- $this->active_user->setToken(make_string(40));
- $save = $this->active_user->save();
- if($save && !is_error($save)) {
- flash_success('API key updated');
- } else {
- flash_error('Failed to update API key. Try again in a few minutes');
- } // if
- $this->redirectToUrl($this->active_user->getApiSettingsUrl());
- } else {
- $this->httpError(HTTP_ERR_BAD_REQUEST);
- } // if
- } // api_reset_key
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement