From 18e2510c29e40a2ba0ffb5e20f289565086bc9b8 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Tue, 17 Apr 2018 14:03:35 +0200 Subject: [PATCH] Add admin UserController --- Changelog | 1 + app/Http/Controllers/Admin/UserController.php | 232 ++++++++++++++++++ app/Models/User.php | 22 +- public/admin/book-edit.php | 55 ----- public/admin/user-delete.php | 16 -- public/admin/user-edit.php | 135 ---------- public/admin/user-list.php | 71 ------ 7 files changed, 242 insertions(+), 290 deletions(-) create mode 100644 app/Http/Controllers/Admin/UserController.php delete mode 100644 public/admin/book-edit.php delete mode 100644 public/admin/user-delete.php delete mode 100644 public/admin/user-edit.php delete mode 100644 public/admin/user-list.php diff --git a/Changelog b/Changelog index 51f556a41..5f38b64fe 100755 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ 2018-04-17 DariusIII + * Chg: Add admin UserController * Chg: Add BookController and edit Blacklist/Anidb class * Chg: Add admin CategoryController * Chg: Add admin anidb and blacklist controllers diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php new file mode 100644 index 000000000..f650da021 --- /dev/null +++ b/app/Http/Controllers/Admin/UserController.php @@ -0,0 +1,232 @@ +setAdminPrefs(); + + $title = 'User List'; + + $roles = []; + foreach (UserRole::getRoles() as $userRole) { + $roles[$userRole['id']] = $userRole['name']; + } + + $ordering = getUserBrowseOrdering(); + $orderBy = $request->has('ob') && \in_array($request->input('ob'), $ordering, false) ? $request->input('ob') : ''; + + $variables = [ + 'username' => $request->has('username') ? $request->input('username') : '', + 'email' => $request->has('email') ? $request->input('email') : '', + 'host' => $request->has('host') ? $request->input('host') : '', + 'role' => $request->has('role') ? $request->input('role') : '', + ]; + + $this->smarty->assign( + [ + 'username' => $variables['username'], + 'email' => $variables['email'], + 'host' => $variables['host'], + 'role' => $variables['role'], + 'role_ids' => array_keys($roles), + 'role_names' => $roles, + 'userlist' => User::getRange( + $orderBy, + $variables['username'], + $variables['email'], + $variables['host'], + $variables['role'] + ), + ] + ); + + User::updateExpiredRoles(); + + foreach ($ordering as $orderType) { + $this->smarty->assign('orderby'.$orderType, WWW_TOP.'/user-list.php?ob='.$orderType.'&offset=0'); + } + + $content = $this->smarty->fetch('user-list.tpl'); + $this->smarty->assign( + [ + 'title' => $title, + 'content' => $content, + ] + ); + + $this->adminrender(); + } + + + /** + * @param \Illuminate\Http\Request $request + * + * @throws \Exception + */ + public function edit(Request $request) + { + $this->setAdminPrefs(); + + $user = [ + 'id' => '', + 'username' => '', + 'email' => '', + 'password' => '', + 'role' => User::ROLE_USER, + 'notes' => '', + ]; + + // set the current action + $action = $request->input('action') ?? 'view'; + + //get the user roles + $userRoles = UserRole::getRoles(); + $roles = []; + $defaultRole = User::ROLE_USER; + $defaultInvites = Invitation::DEFAULT_INVITES; + foreach ($userRoles as $r) { + $roles[$r['id']] = $r['name']; + if ($r['isdefault'] === 1) { + $defaultRole = $r['id']; + $defaultInvites = $r['defaultinvites']; + } + } + + switch ($action) { + case 'add': + $user += [ + 'role' => $defaultRole, + 'notes' => '', + 'invites' => $defaultInvites, + 'movieview' => 0, + 'xxxview' => 0, + 'musicview' => 0, + 'consoleview' => 0, + 'gameview' => 0, + 'bookview' => 0, + ]; + $this->smarty->assign('user', $user); + break; + case 'submit': + if (empty($request->input('id'))) { + $invites = $defaultInvites; + foreach ($userRoles as $role) { + if ($role['id'] === $request->input('role')) { + $invites = $role['defaultinvites']; + } + } + $ret = User::signup($request->input('username'), $request->input('password'), $request->input('email'), '', $request->input('role'), $request->input('notes'), $invites, '', true); + $this->smarty->assign('role', $request->input('role')); + } else { + $ret = User::updateUser($request->input('id'), $request->input('username'), $request->input('email'), $request->input('grabs'), $request->input('role'), $request->input('notes'), $request->input('invites'), ($request->has('movieview') ? 1 : 0), ($request->has('musicview') ? 1 : 0), ($request->has('gameview') ? 1 : 0), ($request->has('xxxview') ? 1 : 0), ($request->has('consoleview') ? 1 : 0), ($request->has('bookview') ? 1 : 0)); + if ($request->input('password') !== null) { + User::updatePassword($request->input('id'), $request->input('password')); + } + if ($request->input('rolechangedate') !== null) { + User::updateUserRoleChangeDate($request->input('id'), $request->input('rolechangedate')); + } + if ($request->input('role') !== null) { + UserRole::query()->where('id', $request->input('role'))->value('name'); + $email = $request->input('email') ?? $request->input('email'); + Mail::to($email)->send(new AccountChange($request->input('id'))); + } + } + + if ($ret >= 0) { + return redirect('user-list'); + } + + switch ($ret) { + case User::ERR_SIGNUP_BADUNAME: + $this->smarty->assign('error', 'Bad username. Try a better one.'); + break; + case User::ERR_SIGNUP_BADPASS: + $this->smarty->assign('error', 'Bad password. Try a longer one.'); + break; + case User::ERR_SIGNUP_BADEMAIL: + $this->smarty->assign('error', 'Bad email.'); + break; + case User::ERR_SIGNUP_UNAMEINUSE: + $this->smarty->assign('error', 'Username in use.'); + break; + case User::ERR_SIGNUP_EMAILINUSE: + $this->smarty->assign('error', 'Email in use.'); + break; + default: + $this->smarty->assign('error', 'Unknown save error.'); + break; + } + $user += [ + 'id' => $request->input('id'), + 'username' => $request->input('username'), + 'email' => $request->input('email'), + 'role' => $request->input('role'), + 'notes' => $request->input('notes'), + ]; + $this->smarty->assign('user', $user); + break; + case 'view': + default: + if ($request->has('id')) { + $title = 'User Edit'; + $id = $request->input('id'); + $user = User::find($id); + + $this->smarty->assign('user', $user); + } + + break; + } + + $this->smarty->assign('yesno_ids', [1, 0]); + $this->smarty->assign('yesno_names', ['Yes', 'No']); + + $this->smarty->assign('role_ids', array_keys($roles)); + $this->smarty->assign('role_names', $roles); + $this->smarty->assign('user', $user); + + $content = $this->smarty->fetch('user-edit.tpl'); + + $this->smarty->assign( + [ + 'title' => $title, + 'content' => $content, + ] + ); + + $this->adminrender(); + } + + /** + * @param \Illuminate\Http\Request $request + * + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + * @throws \Exception + */ + public function destroy(Request $request) + { + if ($request->has('id')) { + User::deleteUser($request->input('id')); + } + + if ($request->has('redir')) { + return redirect($request->input('redir')); + } + + return redirect($request->server('HTTP_REFERER')); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index bc127b3ae..9dd68c27e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -370,17 +370,16 @@ class User extends Authenticatable } /** - * @param $start - * @param $offset - * @param $orderBy + * @param $orderBy * @param string $userName * @param string $email * @param string $host * @param string $role - * @return \Illuminate\Database\Eloquent\Collection|static[] + * + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator * @throws \Exception */ - public static function getRange($start, $offset, $orderBy, $userName = '', $email = '', $host = '', $role = '') + public static function getRange($orderBy, $userName = '', $email = '', $host = '', $role = '') { UserRequest::clearApiRequests(false); @@ -403,11 +402,7 @@ class User extends Authenticatable $users->where('user_roles_id', $role); } - if ($start !== false) { - $users->limit($offset)->offset($start); - } - - return $users->get(); + return $users->paginate(config('nntmux.items_per_page')); } /** @@ -519,10 +514,11 @@ class User extends Authenticatable /** * Check if the user is in the database, and if their API key is good, return user data if so. * - * @param int $userID ID of the user. - * @param string $rssToken API key. * - * @return bool|array + * @param $userID + * @param $rssToken + * + * @return bool|\Illuminate\Database\Eloquent\Model|null|static */ public static function getByIdAndRssToken($userID, $rssToken) { diff --git a/public/admin/book-edit.php b/public/admin/book-edit.php deleted file mode 100644 index 34b718b41..000000000 --- a/public/admin/book-edit.php +++ /dev/null @@ -1,55 +0,0 @@ -setAdminPrefs(); -$book = new Books(); -$gen = new Genres(); -$id = 0; - -// set the current action -$action = request()->input('action') ?? 'view'; - -if (request()->has('id')) { - $id = request()->input('id'); - $b = $book->getBookInfo($id); - - if (! $b) { - $page->show404(); - } - - switch ($action) { - case 'submit': - $coverLoc = WWW_DIR.'covers/book/'.$id.'.jpg'; - - if ($_FILES['cover']['size'] > 0) { - $tmpName = $_FILES['cover']['tmp_name']; - $file_info = getimagesize($tmpName); - if (! empty($file_info)) { - move_uploaded_file($_FILES['cover']['tmp_name'], $coverLoc); - } - } - - request()->merge(['cover' => file_exists($coverLoc) ? 1 : 0]); - request()->merge(['publishdate' => (empty(request()->input('publishdate')) || ! strtotime(request()->input('publishdate'))) ? $con['publishdate'] : Carbon::parse(request()->input('publishdate'))->timestamp]); - $book->update($id, request()->input('title'), request()->input('asin'), request()->input('url'), request()->input('author'), request()->input('publisher'), request()->input('publishdate'), request()->input('cover')); - - header('Location:'.WWW_TOP.'/book-list.php'); - die(); - break; - case 'view': - default: - $page->title = 'Book Edit'; - $page->smarty->assign('book', $b); - break; - } -} - -$page->content = $page->smarty->fetch('book-edit.tpl'); -$page->adminrender(); diff --git a/public/admin/user-delete.php b/public/admin/user-delete.php deleted file mode 100644 index bc5183cad..000000000 --- a/public/admin/user-delete.php +++ /dev/null @@ -1,16 +0,0 @@ -has('id')) { - User::deleteUser(request()->input('id')); -} - -if (request()->has('redir')) { - header('Location: '.request()->input('redir')); -} else { - $referrer = request()->server('HTTP_REFERER'); - header('Location: '.$referrer); -} diff --git a/public/admin/user-edit.php b/public/admin/user-edit.php deleted file mode 100644 index d68577aca..000000000 --- a/public/admin/user-edit.php +++ /dev/null @@ -1,135 +0,0 @@ -setAdminPrefs(); - -$user = [ - 'id' => '', - 'username' => '', - 'email' => '', - 'password' => '', - 'role' => User::ROLE_USER, - 'notes' => '', -]; - -// set the current action -$action = request()->input('action') ?? 'view'; - -//get the user roles -$userRoles = UserRole::getRoles(); -$roles = []; -$defaultRole = User::ROLE_USER; -$defaultInvites = Invitation::DEFAULT_INVITES; -foreach ($userRoles as $r) { - $roles[$r['id']] = $r['name']; - if ($r['isdefault'] === 1) { - $defaultrole = $r['id']; - $defaultinvites = $r['defaultinvites']; - } -} - -switch ($action) { - case 'add': - $user += [ - 'role' => $defaultRole, - 'notes' => '', - 'invites' => $defaultInvites, - 'movieview' => 0, - 'xxxview' => 0, - 'musicview' => 0, - 'consoleview' => 0, - 'gameview' => 0, - 'bookview' => 0, - ]; - $page->smarty->assign('user', $user); - break; - case 'submit': - if (empty(request()->input('id'))) { - $invites = $defaultInvites; - foreach ($userRoles as $role) { - if ($role['id'] === request()->input('role')) { - $invites = $role['defaultinvites']; - } - } - $ret = User::signup(request()->input('username'), request()->input('password'), request()->input('email'), '', request()->input('role'), request()->input('notes'), $invites, '', true); - $page->smarty->assign('role', request()->input('role')); - } else { - $ret = User::updateUser(request()->input('id'), request()->input('username'), request()->input('email'), request()->input('grabs'), request()->input('role'), request()->input('notes'), request()->input('invites'), (request()->has('movieview') ? 1 : 0), (request()->has('musicview') ? 1 : 0), (request()->has('gameview') ? 1 : 0), (request()->has('xxxview') ? 1 : 0), (request()->has('consoleview') ? 1 : 0), (request()->has('bookview') ? 1 : 0)); - if (request()->input('password') !== null) { - User::updatePassword(request()->input('id'), request()->input('password')); - } - if (request()->input('rolechangedate') !== null) { - User::updateUserRoleChangeDate(request()->input('id'), request()->input('rolechangedate')); - } - if (request()->input('role') !== null) { - $newRole = UserRole::query()->where('id', request()->input('role'))->value('name'); - $email = request()->input('email') ?? request()->input('email'); - Mail::to($email)->send(new AccountChange(request()->input('id'))); - } - } - - if ($ret >= 0) { - header('Location:'.WWW_TOP.'/user-list.php'); - } else { - switch ($ret) { - case User::ERR_SIGNUP_BADUNAME: - $page->smarty->assign('error', 'Bad username. Try a better one.'); - break; - case User::ERR_SIGNUP_BADPASS: - $page->smarty->assign('error', 'Bad password. Try a longer one.'); - break; - case User::ERR_SIGNUP_BADEMAIL: - $page->smarty->assign('error', 'Bad email.'); - break; - case User::ERR_SIGNUP_UNAMEINUSE: - $page->smarty->assign('error', 'Username in use.'); - break; - case User::ERR_SIGNUP_EMAILINUSE: - $page->smarty->assign('error', 'Email in use.'); - break; - default: - $page->smarty->assign('error', 'Unknown save error.'); - break; - } - $user += [ - 'id' => request()->input('id'), - 'username' => request()->input('username'), - 'email' => request()->input('email'), - 'role' => request()->input('role'), - 'notes' => request()->input('notes'), - ]; - $page->smarty->assign('user', $user); - } - break; - case 'view': - default: - - if (request()->has('id')) { - $page->title = 'User Edit'; - $id = request()->input('id'); - $user = User::find($id); - - $page->smarty->assign('user', $user); - } - - break; -} - -$page->smarty->assign('yesno_ids', [1, 0]); -$page->smarty->assign('yesno_names', ['Yes', 'No']); - -$page->smarty->assign('role_ids', array_keys($roles)); -$page->smarty->assign('role_names', $roles); -$page->smarty->assign('user', $user); - -$page->content = $page->smarty->fetch('user-edit.tpl'); -$page->adminrender(); diff --git a/public/admin/user-list.php b/public/admin/user-list.php deleted file mode 100644 index bbfa4d039..000000000 --- a/public/admin/user-list.php +++ /dev/null @@ -1,71 +0,0 @@ -setAdminPrefs(); - -$page->title = 'User List'; - -$roles = []; -foreach (UserRole::getRoles() as $userRole) { - $roles[$userRole['id']] = $userRole['name']; -} - -$offset = request()->input('offset') ?? 0; -$ordering = getUserBrowseOrdering(); -$orderBy = request()->has('ob') && in_array(request()->input('ob'), $ordering, false) ? request()->input('ob') : ''; - -$variables = ['username' => '', 'email' => '', 'host' => '', 'role' => '']; -$uSearch = ''; -foreach ($variables as $key => $variable) { - checkREQUEST($key); -} - -$page->smarty->assign( - [ - 'username' => $variables['username'], - 'email' => $variables['email'], - 'host' => $variables['host'], - 'role' => $variables['role'], - 'role_ids' => array_keys($roles), - 'role_names' => $roles, - 'pagerquerysuffix' => '#results', - 'pagertotalitems' => User::getCount($variables['role'], $variables['username'], $variables['host'], $variables['email']), - 'pageroffset' => $offset, - 'pageritemsperpage' => config('nntmux.items_per_page'), - 'pagerquerybase' => WWW_TOP.'/user-list.php?ob='.$orderBy.$uSearch.'&offset=', - 'userlist' => User::getRange( - $offset, - config('nntmux.items_per_page'), - $orderBy, - $variables['username'], - $variables['email'], - $variables['host'], - $variables['role'] - ), - ] -); - -User::updateExpiredRoles(); - -foreach ($ordering as $orderType) { - $page->smarty->assign('orderby'.$orderType, WWW_TOP.'/user-list.php?ob='.$orderType.'&offset=0'); -} - -$page->smarty->assign('pager', $page->smarty->fetch('pager.tpl')); -$page->content = $page->smarty->fetch('user-list.tpl'); -$page->adminrender(); - -function checkREQUEST($param) -{ - global $uSearch, $variables; - if (isset($_REQUEST[$param])) { - $variables[$param] = $_REQUEST[$param]; - $uSearch .= "&$param=".$_REQUEST[$param]; - } -}