Add email to create user, in setting edit user permission and emailmanager file

This commit is contained in:
haitem 2020-10-17 18:03:55 +02:00
parent 740a2debf7
commit 6b7bd911f7
8 changed files with 56 additions and 7 deletions

View File

@ -1,16 +1,18 @@
<?php <?php
if (isset($_POST) && !empty($_POST)){ if (isset($_POST) && !empty($_POST)){
$userManager = new UserManager();
if (isset($_POST['submitPasswordChange']) && $_POST['submitPasswordChange'] != "") { if (isset($_POST['submitPasswordChange']) && $_POST['submitPasswordChange'] != "") {
$oldPassword = $_POST['oldPassword']; $oldPassword = $_POST['oldPassword'];
$newPassword = $_POST['newPassword1']; $newPassword = $_POST['newPassword1'];
$newPassword2 = $_POST['newPassword2']; $newPassword2 = $_POST['newPassword2'];
UserManager::changePassword($oldPassword, $newPassword, $newPassword2); $userManager->changePassword($oldPassword, $newPassword, $newPassword2);
header('Location: ' . BASEURL . 'logout'); header('Location: ' . BASEURL . 'logout');
die(); die();
} else if (isset($_POST['submitCreateUser']) && $_POST['submitCreateUser'] != "") { } else if (isset($_POST['submitCreateUser']) && $_POST['submitCreateUser'] != "") {
$userName = $_POST['userName']; $userName = $_POST['userName'];
$password = $_POST['userPassword']; $password = $_POST['userPassword'];
UserManager::createUser($userName, $password); $email = $_POST['userEmail'];
$userManager->createUser($userName, $password, $email);
header('Location: ' . BASEURL . 'setting'); header('Location: ' . BASEURL . 'setting');
die(); die();
} else if (isset($_POST['submitEnableOta']) && $_POST['submitEnableOta'] != "") { } else if (isset($_POST['submitEnableOta']) && $_POST['submitEnableOta'] != "") {
@ -20,9 +22,13 @@ if (isset($_POST) && !empty($_POST)){
$ga = new PHPGangsta_GoogleAuthenticator(); $ga = new PHPGangsta_GoogleAuthenticator();
$checkResult = $ga->verifyCode($otaSecret, $otaCode, 2); // 2 = 2*30sec clock tolerance $checkResult = $ga->verifyCode($otaSecret, $otaCode, 2); // 2 = 2*30sec clock tolerance
if ($checkResult) { if ($checkResult) {
UserManager::setOta($otaCode, $otaSecret); $userManager->setOta($otaCode, $otaSecret);
} }
header('Location: ' . BASEURL . 'setting'); header('Location: ' . BASEURL . 'setting');
die(); die();
} else if (isset ($_POST['userPermission']) && !empty ($_POST['userID'])) {
$userManager->setUserDataAdmin("permission", $_POST['userPermission'], $_POST['userID']);
header('Location: ' . BASEURL . 'setting');
die();
} }
} }

View File

@ -0,0 +1,11 @@
<?php
class EmailManager {
public function SendTo ($pro, $predmet, $zprava) {
$email = "From: EnergyCounter@steelants.cz";
$email .= "\nMIME-Version: 1.0\n";
$email .= "Content-Type: text/html; charset=\"utf-8\"\n";
if (!mb_send_mail ($pro, $predmet, $zprava, $email)) {
throw new PDOException("!Email se nepodařilo odeslat!");
}
}
}

View File

@ -43,7 +43,7 @@ class UserManager
public static function login ($username, $password, $rememberMe) { public static function login ($username, $password, $rememberMe) {
try { try {
if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?)', array ($username))) { if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?) OR LOWER(email)=LOWER(?)', array ($username, $username))) {
if ($user['password'] == UserManager::getHashPassword($password)) { if ($user['password'] == UserManager::getHashPassword($password)) {
if (isset($rememberMe) && $rememberMe == 'true') { if (isset($rememberMe) && $rememberMe == 'true') {
setcookie ("rememberMe", self::setEncryptedCookie($user['username']), time () + (30 * 24 * 60 * 60 * 1000), BASEDIR, $_SERVER['HTTP_HOST'], 1); setcookie ("rememberMe", self::setEncryptedCookie($user['username']), time () + (30 * 24 * 60 * 60 * 1000), BASEDIR, $_SERVER['HTTP_HOST'], 1);
@ -69,7 +69,7 @@ class UserManager
public static function loginNew ($username, $password) { public static function loginNew ($username, $password) {
try { try {
if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?)', array ($username))) { if ($user = Db::loadOne ('SELECT * FROM users WHERE LOWER(username)=LOWER(?) OR LOWER(email)=LOWER(?)', array ($username, $username))) {
if ($user['password'] == UserManager::getHashPassword($password)) { if ($user['password'] == UserManager::getHashPassword($password)) {
return $user['user_id']; return $user['user_id'];
} else { } else {
@ -152,6 +152,12 @@ class UserManager
} }
} }
public static function setUserDataAdmin ($type, $value, $id) {
if ($id) {
Db::command ('UPDATE users SET ' . $type . '=? WHERE user_id=?', array ($value, $id));
}
}
public static function getHashPassword ($password) { public static function getHashPassword ($password) {
$salt = "s0mRIdlKvI"; $salt = "s0mRIdlKvI";
$hashPassword = hash('sha512', ($password . $salt)); $hashPassword = hash('sha512', ($password . $salt));
@ -181,8 +187,9 @@ class UserManager
} }
} }
public static function createUser($userName, $password){ public static function createUser ($userName, $password, $email) {
$userId = Db::loadOne('SELECT * FROM users WHERE username = ?;', array($userName))['user_id']; $email = strtolower ($email);
$userId = Db::loadOne ('SELECT * FROM users WHERE LOWER (username) = LOWER (?) OR LOWER (email) = LOWER (?);', array ($userName, $email))['user_id'];
if ($userId != null) { if ($userId != null) {
return false; return false;
}; };
@ -190,6 +197,7 @@ class UserManager
$user = [ $user = [
'username' => $userName, 'username' => $userName,
'password' => self::getHashPassword($password), 'password' => self::getHashPassword($password),
'email' => $email,
]; ];
return Db::add ('users', $user); return Db::add ('users', $user);
} catch(PDOException $error) { } catch(PDOException $error) {

View File

@ -117,6 +117,7 @@
<th><?php $LANGMNG->echo('t_avatar');?></th> <th><?php $LANGMNG->echo('t_avatar');?></th>
<th><?php $LANGMNG->echo('t_userName');?></th> <th><?php $LANGMNG->echo('t_userName');?></th>
<th><?php $LANGMNG->echo('t_ota');?></th> <th><?php $LANGMNG->echo('t_ota');?></th>
<th><?php $LANGMNG->echo('t_permission');?></th>
<th><?php $LANGMNG->echo('t_action');?></th> <th><?php $LANGMNG->echo('t_action');?></th>
</tr> </tr>
</thead> </thead>
@ -126,6 +127,15 @@
<td><img src="<?php echo $user['gavatar_url']; ?>" /></td> <td><img src="<?php echo $user['gavatar_url']; ?>" /></td>
<td><?php echo $user['username']; ?></td> <td><?php echo $user['username']; ?></td>
<td><?php echo ($user['ota'] ? '<span class="fa">&#xf00c;</span>' : ''); ?></td> <td><?php echo ($user['ota'] ? '<span class="fa">&#xf00c;</span>' : ''); ?></td>
<td>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="userID" value="<?php echo $user['user_id']; ?>"/>
<select class="input" name="userPermission" onchange="this.form.submit();">
<option value="0"<?php echo (empty ($user['permission']) ? " selected" : "") ?>><?php $LANGMNG->echo('t_permission_user'); ?></option>
<option value="1"<?php echo (!empty ($user['permission']) ? " selected" : "") ?>><?php $LANGMNG->echo('t_permission_admin'); ?></option>
</select>
</form>
</td>
<td><button name="deleteUser" type="button" class="button is-danger fa">&#xf1f8;</button></td> <td><button name="deleteUser" type="button" class="button is-danger fa">&#xf1f8;</button></td>
</tr> </tr>
<?php } ?> <?php } ?>
@ -143,6 +153,10 @@
<div class="label"><?php $LANGMNG->echo('l_password') ?>:</div> <div class="label"><?php $LANGMNG->echo('l_password') ?>:</div>
<input type="password" class="input" name="userPassword" value=""> <input type="password" class="input" name="userPassword" value="">
</div> </div>
<div class="field">
<div class="label"><?php $LANGMNG->echo('l_email') ?>:</div>
<input type="email" class="input" name="userEmail" value="">
</div>
<div class="field"> <div class="field">
<input type="submit" name="submitCreateUser" class="button" value="Uložit"> <input type="submit" name="submitCreateUser" class="button" value="Uložit">
</div> </div>

View File

@ -57,6 +57,7 @@ return $lang = [
'l_notificationStatus' => 'Notification status', 'l_notificationStatus' => 'Notification status',
'l_userName' => 'Uživatelské jméno', 'l_userName' => 'Uživatelské jméno',
'l_password' => 'Heslo', 'l_password' => 'Heslo',
'l_email' => 'Email',
'l_oldPassword' => 'Staré Heslo', 'l_oldPassword' => 'Staré Heslo',
'l_newPassword' => 'Nové Heslo', 'l_newPassword' => 'Nové Heslo',
'l_uploadFirmware' => 'Nahrát Firmware', 'l_uploadFirmware' => 'Nahrát Firmware',
@ -79,6 +80,9 @@ return $lang = [
't_networkSetting' => 'Nastavení Sítě', 't_networkSetting' => 'Nastavení Sítě',
't_deviceVersion' => 'Nastavení Verze', 't_deviceVersion' => 'Nastavení Verze',
't_ota' => 'OTA', 't_ota' => 'OTA',
't_permission' => 'Oprávnění',
't_permission_user' => 'Uživatel',
't_permission_admin' => 'Administrátor',
't_listUsers' => 'Seznam Uživatelů', 't_listUsers' => 'Seznam Uživatelů',
't_avatar' => 'Avatar', 't_avatar' => 'Avatar',
't_listRooms' => 'Seznam Místností', 't_listRooms' => 'Seznam Místností',

View File

@ -57,6 +57,7 @@ return $lang = [
'l_notificationStatus' => 'Notification status', 'l_notificationStatus' => 'Notification status',
'l_userName' => 'Username', 'l_userName' => 'Username',
'l_password' => 'Password', 'l_password' => 'Password',
'l_email' => 'Email',
'l_oldPassword' => 'Old Password', 'l_oldPassword' => 'Old Password',
'l_newPassword' => 'New Password', 'l_newPassword' => 'New Password',
'l_uploadFirmware' => 'Upload Firmware', 'l_uploadFirmware' => 'Upload Firmware',
@ -79,6 +80,9 @@ return $lang = [
't_networkSetting' => 'Network Setting', 't_networkSetting' => 'Network Setting',
't_deviceVersion' => 'Version Setting', 't_deviceVersion' => 'Version Setting',
't_ota' => 'OTA', 't_ota' => 'OTA',
't_permission' => 'Permission',
't_permission_user' => 'User',
't_permission_admin' => 'Admin',
't_listUsers' => 'User List', 't_listUsers' => 'User List',
't_avatar' => 'Avatar', 't_avatar' => 'Avatar',
't_listRooms' => 'Room List', 't_listRooms' => 'Room List',

View File

@ -52,6 +52,7 @@ return $lang = [
'l_sleepTime' => 'Apparaat slaaptijd', 'l_sleepTime' => 'Apparaat slaaptijd',
'l_atHome' => 'Thuis', 'l_atHome' => 'Thuis',
'l_nameAt' => 'Naam', 'l_nameAt' => 'Naam',
'l_email' => 'Email',
'l_lastSeen' => 'Laatst gezien', 'l_lastSeen' => 'Laatst gezien',
'l_notificationStatus' => 'Notificatie status', 'l_notificationStatus' => 'Notificatie status',

View File

@ -56,6 +56,7 @@ return $lang = [
'l_notificationStatus' => 'Stan powiadomienia', 'l_notificationStatus' => 'Stan powiadomienia',
'l_userName' => 'Username', //newOne 'l_userName' => 'Username', //newOne
'l_password' => 'Password', //newOne 'l_password' => 'Password', //newOne
'l_email' => 'Email',
'l_oldPassword' => 'Old Password', //newOne 'l_oldPassword' => 'Old Password', //newOne
'l_newPassword' => 'New Password', //newOne 'l_newPassword' => 'New Password', //newOne