issue #11: adding profile page

This commit is contained in:
Pepijn Over 2014-03-15 21:47:55 +01:00
parent 416eff8875
commit b45e3699d4
16 changed files with 222 additions and 8 deletions

View File

@ -343,9 +343,10 @@ function psm_build_mail($from_name = null, $from_email = null) {
* Generate a new link to the current monitor
* @param array $params key value pairs
* @param boolean $urlencode urlencode all params?
* @param boolean $htmlentities use entities in url?
* @return string
*/
function psm_build_url($params = array(), $urlencode = true) {
function psm_build_url($params = array(), $urlencode = true, $htmlentities = true) {
$defports = array(80, 443);
$url = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
if(!in_array($_SERVER['SERVER_PORT'], $defports)) {
@ -355,12 +356,13 @@ function psm_build_url($params = array(), $urlencode = true) {
if($params != null) {
$url .= '?';
$delim = ($htmlentities) ? '&' : '&';
foreach($params as $k => $v) {
if($urlencode) {
$v = urlencode($v);
}
$url .= '&' . $k . '=' . $v;
$url .= $delim . $k . '=' . $v;
}
}

View File

@ -68,6 +68,8 @@ $sm_lang = array(
'email' => 'Имейл',
'updated' => 'Информацията за потребителя е обновена.',
'inserted' => 'Потребителят е добавен.',
'profile' => 'Profile',
'profile_updated' => 'Your profile has been updated.',
'error_user_name_bad_length' => 'Usernames must be between 2 and 64 characters.',
'error_user_name_invalid' => 'It may only contain alphabetic characters (a-z, A-Z), digits (0-9) and underscores (_).',
'error_user_name_exists' => 'The given username already exists in the database.',

View File

@ -68,6 +68,8 @@ $sm_lang = array(
'email' => 'Email',
'updated' => 'Usuário atualizado.',
'inserted' => 'Usuário adicionado.',
'profile' => 'Profile',
'profile_updated' => 'Your profile has been updated.',
'error_user_name_bad_length' => 'Usernames must be between 2 and 64 characters.',
'error_user_name_invalid' => 'It may only contain alphabetic characters (a-z, A-Z), digits (0-9) and underscores (_).',
'error_user_name_exists' => 'The given username already exists in the database.',

View File

@ -68,6 +68,8 @@ $sm_lang = array(
'email' => 'Email',
'updated' => 'Benutzer bearbeitet.',
'inserted' => 'Benutzer eingetragen.',
'profile' => 'Profile',
'profile_updated' => 'Your profile has been updated.',
'error_user_name_bad_length' => 'Usernames must be between 2 and 64 characters.',
'error_user_name_invalid' => 'It may only contain alphabetic characters (a-z, A-Z), digits (0-9) and underscores (_).',
'error_user_name_exists' => 'The given username already exists in the database.',

View File

@ -68,8 +68,10 @@ $sm_lang = array(
'email' => 'Email',
'updated' => 'User updated.',
'inserted' => 'User added.',
'profile' => 'Profile',
'profile_updated' => 'Your profile has been updated.',
'error_user_name_bad_length' => 'Usernames must be between 2 and 64 characters.',
'error_user_name_invalid' => 'It may only contain alphabetic characters (a-z, A-Z), digits (0-9) and underscores (_).',
'error_user_name_invalid' => 'The username may only contain alphabetic characters (a-z, A-Z), digits (0-9) and underscores (_).',
'error_user_name_exists' => 'The given username already exists in the database.',
'error_user_email_bad_length' => 'Email addresses must be between 5 and 255 characters.',
'error_user_email_invalid' => 'The email address is invalid.',

View File

@ -68,6 +68,8 @@ $sm_lang = array(
'email' => 'Email',
'updated' => 'Utilisateur mis à jour.',
'inserted' => 'Utilisateur ajouté.',
'profile' => 'Profile',
'profile_updated' => 'Your profile has been updated.',
'error_user_name_bad_length' => 'Usernames must be between 2 and 64 characters.',
'error_user_name_invalid' => 'It may only contain alphabetic characters (a-z, A-Z), digits (0-9) and underscores (_).',
'error_user_name_exists' => 'The given username already exists in the database.',

View File

@ -68,6 +68,8 @@ $sm_lang = array(
'email' => 'Email',
'updated' => '수정되었습니다.',
'inserted' => '추가되었습니다.',
'profile' => 'Profile',
'profile_updated' => 'Your profile has been updated.',
'error_user_name_bad_length' => 'Usernames must be between 2 and 64 characters.',
'error_user_name_invalid' => 'It may only contain alphabetic characters (a-z, A-Z), digits (0-9) and underscores (_).',
'error_user_name_exists' => 'The given username already exists in the database.',

View File

@ -68,6 +68,8 @@ $sm_lang = array(
'email' => 'Email',
'updated' => 'Gebruiker gewijzigd.',
'inserted' => 'Gebruiker toegevoegd.',
'profile' => 'Profiel',
'profile_updated' => 'Je profiel is bijgewerkt.',
'error_user_name_bad_length' => 'Een gebruikersnaam moet tussen de 2 en 64 tekens zijn.',
'error_user_name_invalid' => 'Een gebruikersnaam mag alleen alfabetische tekens (a-z, A-Z), cijfers (0-9) en underscores (_) bevatten.',
'error_user_name_exists' => 'De opgegeven gebruikersnaam bestaat al.',

View File

@ -207,7 +207,9 @@ abstract class AbstractController implements ControllerInterface {
$tpl_data = array(
'label_help' => psm_get_lang('menu', 'help'),
'label_profile' => psm_get_lang('users', 'profile'),
'label_logout' => psm_get_lang('login', 'logout'),
'url_profile' => psm_build_url(array('mod' => 'user_profile')),
'url_logout' => psm_build_url(array('logout' => 1)),
);

View File

@ -48,7 +48,7 @@ class UpdateController extends AbstractController {
// redirect user to regular status page
header('Location: ' . psm_build_url(array(
'mod' => 'server_status'
)));
), true, false));
die();
}

View File

@ -171,7 +171,7 @@ class LoginController extends AbstractController {
'action' => 'reset',
'user_id' => $user_id,
'token' => $user_password_reset_hash,
));
), true, false);
$body = psm_get_lang('login', 'password_reset_email_body');
$body = str_replace('%link%', $url, $body);
$mail->Body = $body;

View File

@ -0,0 +1,142 @@
<?php
/**
* PHP Server Monitor
* Monitor your servers and websites.
*
* This file is part of PHP Server Monitor.
* PHP Server Monitor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PHP Server Monitor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Server Monitor. If not, see <http://www.gnu.org/licenses/>.
*
* @package phpservermon
* @author Pepijn Over <pep@neanderthal-technology.com>
* @copyright Copyright (c) 2008-2014 Pepijn Over <pep@neanderthal-technology.com>
* @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3
* @version Release: @package_version@
* @since phpservermon 2.2.0
**/
namespace psm\Module\User\Controller;
use psm\Module\AbstractController;
use psm\Service\Database;
use psm\Service\Template;
class ProfileController extends AbstractController {
/**
* Editable fields for the profile
* @var array $profile_fields
*/
protected $profile_fields = array('name', 'user_name', 'mobile', 'email');
function __construct(Database $db, Template $tpl) {
parent::__construct($db, $tpl);
$this->setActions(array(
'index', 'save',
), 'index');
}
/**
* Show the profile page
*/
protected function executeIndex() {
$this->setTemplateId('user_profile', 'user/profile.tpl.html');
$user = $this->user->getUser(null, true);
$tpl_data = array(
'form_action' => psm_build_url(array(
'mod' => 'user_profile',
'action' => 'save',
)),
'level' => psm_get_lang('users', 'level_' . $user->level),
'placeholder_password' => psm_get_lang('users', 'password_leave_blank'),
);
foreach($this->profile_fields as $field) {
$tpl_data[$field] = (isset($user->$field)) ? $user->$field : '';
}
$this->tpl->addTemplateData($this->getTemplateId(), $tpl_data);
}
/**
* Save the profile
*/
protected function executeSave() {
if(empty($_POST)) {
// dont process anything if no data has been posted
return $this->executeIndex();
}
$validator = new \psm\Util\User\UserValidator($this->user);
$user = $this->user->getUser();
$fields = $this->profile_fields;
$fields[] = 'password';
$fields[] = 'password_repeat';
$clean = array();
foreach($fields as $field) {
if(isset($_POST[$field])) {
$clean[$field] = trim(strip_tags($_POST[$field]));
} else {
$clean[$field] = '';
}
}
// validate the lot
try {
$validator->username($clean['user_name'], $this->user->getUserId());
$validator->email($clean['email']);
// always validate password for new users,
// but only validate it for existing users when they change it.
if($clean['password'] != '') {
$validator->password($clean['password'], $clean['password_repeat']);
}
} catch(\InvalidArgumentException $e) {
$this->addMessage(psm_get_lang('users', 'error_' . $e->getMessage()), 'error');
return $this->executeIndex();
}
if(!empty($clean['password'])) {
$password = $clean['password'];
}
unset($clean['password']);
unset($clean['password_repeat']);
$this->db->save(PSM_DB_PREFIX.'users', $clean, array('user_id' => $this->user->getUserId()));
if(isset($password)) {
$this->user->changePassword($this->user->getUserId(), $password);
}
$this->addMessage(psm_get_lang('users', 'profile_updated'), 'success');
return $this->executeIndex();
}
// override parent::createHTMLLabels()
protected function createHTMLLabels() {
$this->tpl->addTemplateData(
$this->getTemplateId(),
array(
'subtitle' => psm_get_lang('users', 'profile'),
'label_name' => psm_get_lang('users', 'name'),
'label_user_name' => psm_get_lang('users', 'user_name'),
'label_password' => psm_get_lang('users', 'password'),
'label_password_repeat' => psm_get_lang('users', 'password_repeat'),
'label_level' => psm_get_lang('users', 'level'),
'label_mobile' => psm_get_lang('users', 'mobile'),
'label_email' => psm_get_lang('users', 'email'),
'label_save' => psm_get_lang('system', 'save'),
)
);
return parent::createHTMLLabels();
}
}

View File

@ -34,7 +34,7 @@ class UserModule implements ModuleInterface {
return array(
'user' => __NAMESPACE__ . '\Controller\UserController',
'login' => __NAMESPACE__ . '\Controller\LoginController',
// 'profile' => __NAMESPACE__ . '\Controller\StatusController',
'profile' => __NAMESPACE__ . '\Controller\ProfileController',
);
}

View File

@ -93,9 +93,11 @@ class User {
/**
* Get user by id, or get current user.
* @param int $user_id if null it will attempt current user id
* @param boolean $flush if TRUE it will query db regardless of whether we already have the data
* @return object|boolean FALSE if user not found, object otherwise
*/
public function getUser($user_id = null) {
public function getUser($user_id = null, $flush = false) {
if($user_id == null) {
if(!$this->isUserLoggedIn()) {
return false;
@ -104,7 +106,7 @@ class User {
}
}
if(!isset($this->user_data[$user_id])) {
if(!isset($this->user_data[$user_id]) || $flush) {
$query_user = $this->db_connection->prepare('SELECT * FROM '.PSM_DB_PREFIX.'users WHERE user_id = :user_id');
$query_user->bindValue(':user_id', $user_id, \PDO::PARAM_INT);
$query_user->execute();

View File

@ -75,6 +75,7 @@
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{label_usermenu} <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="{url_profile}">{label_profile}</a></li>
<li><a href="{url_logout}">{label_logout}</a></li>
</ul>
</li>

View File

@ -0,0 +1,51 @@
<!--%tpl_user_profile-->
<div class="span12">
<form class="form-horizontal well" action="{form_action}" method="post">
<fieldset>
<div class="control-group">
<label class="control-label" for="name">{label_name}</label>
<div class="controls">
<input type="text" id="name" name="name" value="{name}" maxlength="255" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="user_name">{label_user_name}</label>
<div class="controls">
<input type="text" id="user_name" name="user_name" value="{user_name}" maxlength="64" required>
</div>
</div>
<div class="control-group">
<label class="control-label" for="level">{label_level}</label>
<div class="controls"><input type="text" value="{level}" disabled="disabled" /></div>
</div>
<div class="control-group">
<label class="control-label" for="password">{label_password}</label>
<div class="controls">
<input type="password" id="password" name="password" maxlength="255" placeholder="{placeholder_password}" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="password_repeat">{label_password_repeat}</label>
<div class="controls">
<input type="password" id="password_repeat" name="password_repeat" maxlength="255" placeholder="{placeholder_password}" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="mobile">{label_mobile}</label>
<div class="controls">
<input type="text" id="mobile" name="mobile" value="{mobile}" maxlength="15" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">{label_email}</label>
<div class="controls">
<input type="text" id="email" name="email" value="{email}" maxlength="255" required>
</div>
</div>
<div class="form-actions">
<button class="btn btn-success" type="submit">{label_save}</button>
</div>
</fieldset>
</form>
</div>
<!--%%tpl_user_profile-->