Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Editor - Added user configs #50

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
16 changes: 16 additions & 0 deletions app/Controllers/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ public function __construct () {
$view_parameters['manga_directory'] = $configs_dict['manga_directory'];
$view_parameters['library_view_type'] = $configs_dict['library_view_type'];

$q = '
SELECT *
FROM `user_types`';
$r = \Core\Database::query ($q);

$view_parameters['user_types'] = $r;

$q = '
SELECT *
FROM `users`';
$r = \Core\Database::query ($q);

$view_parameters['users'] = $r;

$view_parameters['user_type'] = (new \Core\SessionManager ())->getSessionItem ('user_type');

$view = new ConfigView ($view_parameters);
$view->render ();
}
Expand Down
4 changes: 3 additions & 1 deletion app/Core/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public function ajaxValidateLogin () {

// Get saved value
$q = '
SELECT `username`, `password` FROM `users`
SELECT `username`, `password`, `type`
FROM `users`
WHERE `username` = "'.\Core\Database::sanitize ($_POST['username']).'"';
$r = \Core\Database::query ($q);

Expand All @@ -35,6 +36,7 @@ public function ajaxValidateLogin () {

if ($pass_valid === true) {
$this->setSessionItem ('username', $_POST['username']);
$this->setSessionItem ('user_type', $r[0]['type']);

return (1);
} else {
Expand Down
60 changes: 60 additions & 0 deletions app/ViewItems/CSS/Config.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
}

.config_list_wrap .config_list .config_section {
margin-bottom: 35px;
background-color: #2b2b2b;
box-shadow: 0 0 4px 4px rgba(0, 0, 0, 0.4);
box-sizing: border-box;
Expand Down Expand Up @@ -56,3 +57,62 @@
cursor: pointer;
}

/*
User config section
*/
.config_wrap .user_header {
display: block;
border-bottom: 2px solid grey;
}

.config_wrap .user_header .header_item,
.config_wrap .user_list .user_column {
display: inline-block;
}

.config_wrap .user_header .header_item.header_username,
.config_wrap .user_list .user_column.column_username {
width: 80%;
}

.config_wrap .user_header .header_item.header_type,
.config_wrap .user_list .user_column.column_type {
width: 20%;
}

.config_wrap .user_list {
display: block;
}

.config_wrap .user_list .user_item {
padding: 5px;
}

.config_wrap .user_list .user_item .user_column .username {
display: inline-block;
}

.config_wrap .user_list .user_item .user_column .user_editor_btn {
margin-right: 10px;
padding: 4px 8px;
display: inline-block;
float: right;
background-color: #1c4870;
cursor: pointer;
color: #fff;
font-size: 0.7em;
}

.config_wrap .add_user_btn {
margin-top: 10px;
padding: 5px 12px;
display: inline-block;
background-color: #00733b;
color: #000;
cursor: pointer;
font-size: 0.9em;
}

.config_wrap .add_user_btn:hover {
background-color: #00cc68;
}
15 changes: 15 additions & 0 deletions app/ViewItems/JS/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,19 @@ $(window).ready (function () {
}
});
});

$(".new_user_row input").keypress (function (e) {
if (e.which === 13) {
let $parent = $(this).parent ();
let value = $(this).val ();
$(this).remove ();
$parent.html (value);
}
});

$(".add_user_btn").click (function () {
let userRow = $(".clones .new_user_row").clone (true);

$(".user_list").append (userRow);
});
});
139 changes: 139 additions & 0 deletions app/ViewItems/PageViews/ConfigView.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,64 @@ protected function constructHTML () {
</div>
</div>
</div>
</div>';
if ($this->getUserType () === 'admin') {
$output .=
'<div class="config_section">
<div class="section_header">User Settings (Admin)</div>
<div class="config_wrap">
<div class="user_header">
<div class="header_item header_username">Username</div><!--
--><div class="header_item header_type">User Type</div>
</div>
<div class="user_list">';
foreach ($this->getUsers () as $user) {
$output .=
'<div class="user_item">
<div class="user_column column_username">
<div class="username">
'.$user['username'].'
</div>
<div class="user_editor_btn change_pass_btn">Change Password</div>
<div class="user_editor_btn change_name_btn">Change Name</div>
</div><!--
--><div class="user_column column_type">
<select>';
foreach ($this->getUserTypes () as $type) {
$output .=
'<option value="'.$type['type_name'].'" '.($user['type'] === $type['type_name'] ? 'selected' : '').'>'.$type['type_name'].'</option>';
}
$output .=
'</select>
</div>
</div>';
}
$output .=
'</div>
<div class="add_user_btn">Add User</div>
</div>
</div>';
}
$output .=
'</div>
</div>
<div class="clones" style="display: none;">
<div class="user_item new_user_row">
<div class="user_column column_username">
<div class="username">
<input type="text" autocomplete="off" placeholder="New Username" />
</div>
<div class="user_editor_btn change_pass_btn">Change Password</div>
<div class="user_editor_btn change_name_btn">Change Name</div>
</div><!--
--><div class="user_column column_type">
<select>';
foreach ($this->getUserTypes () as $type) {
$output .=
'<option value="'.$type['type_name'].'">'.$type['type_name'].'</option>';
}
$output .=
'</select>
</div>
</div>';

Expand Down Expand Up @@ -96,6 +153,33 @@ protected function getReaderDisplayStyle () {
return ($this->reader_display_style);
}

/**
* Gets the list of users.
*
* @return array list of users
*/
protected function getUsers () {
return ($this->users);
}

/**
* Gets the currently logged in user's type.
*
* @return string user's type
*/
protected function getUserType () {
return ($this->user_type);
}

/**
* Gets list of all available user types.
*
* @return array list of user types
*/
protected function getUserTypes () {
return ($this->user_types);
}

/**
* Sets the library view type.
*
Expand Down Expand Up @@ -145,4 +229,59 @@ protected function setReaderDisplayStyle (int $config) {

$this->reader_display_style = $config;
}

/**
* Sets list of server users.
*
* @param array $users list of users
*
* @throws TypeError on non-array parameter
* @throws InvalidArgumentException on missing user keys or non-string items
*/
protected function setUsers (array $users) {
foreach ($users as $user) {
foreach (['username', 'type'] as $key) {
if (!array_key_exists ($key, $user)) {
throw new \InvalidArgumentException ('Argument (Users) items must each have key "'.$key.'".');
}
if (!is_string ($user[$key])) {
throw new \InvalidArgumentException ('Argument (Users) items must all be strings; '.gettype ($user[$key]).' given.');
}
}
}

$this->users = $users;
}

/**
* Sets currently logged in user's type.
*
* @param string $user_type user's type
*
* @throws TypeError on non-string parameter
*/
protected function setUserType (string $user_type) {
$this->user_type = $user_type;
}

/**
* Sets list of server user types.
*
* @param array $user_types list of user types
*
* @throws TypeError on non-array parameter
* @throws InvalidArgumentException on missing user type keys or non-string items
*/
protected function setUserTypes (array $user_types) {
foreach ($user_types as $user_type) {
if (!array_key_exists ('type_name', $user_type)) {
throw new \InvalidArgumentException ('Argument (Users Types) items must each have key "type_name".');
}
if (!is_string ($user_type['type_name'])) {
throw new \InvalidArgumentException ('Argument (User Types) items must all be strings; '.gettype ($user_type['type_name']).' given.');
}
}

$this->user_types = $user_types;
}
}