diff --git a/database/2018_04_17_221700_add_last_seen_and_user_title.php b/database/2018_04_17_221700_add_last_seen_and_user_title.php new file mode 100644 index 00000000..723bc368 --- /dev/null +++ b/database/2018_04_17_221700_add_last_seen_and_user_title.php @@ -0,0 +1,39 @@ +getSchemaBuilder(); + $schema->table('users', function (Blueprint $table) { + $table->string('user_title', 64) + ->nullable() + ->default(null); + + $table->timestamp('last_seen') + ->nullable() + ->default(null); + }); + } + + /** + * @SuppressWarnings(PHPMD) + */ + public function down() + { + $schema = Database::connection()->getSchemaBuilder(); + $schema->table('users', function (Blueprint $table) { + $table->dropColumn([ + 'user_title', + 'last_seen', + ]); + }); + } +} diff --git a/misuzu.php b/misuzu.php index 9370023a..4dfdcf44 100644 --- a/misuzu.php +++ b/misuzu.php @@ -31,6 +31,13 @@ if (PHP_SAPI !== 'cli') { if (isset($_COOKIE['msz_uid'], $_COOKIE['msz_sid'])) { $app->startSession((int)$_COOKIE['msz_uid'], $_COOKIE['msz_sid']); + $session = $app->getSession(); + + if ($session !== null) { + $session->user->last_seen = \Carbon\Carbon::now(); + $session->user->last_ip = \Misuzu\Net\IPAddress::remote(); + $session->user->save(); + } } $manage_mode = starts_with($_SERVER['REQUEST_URI'], '/manage'); @@ -39,7 +46,7 @@ if (PHP_SAPI !== 'cli') { $app->templating->addPath('mio', __DIR__ . '/views/mio'); if ($manage_mode) { - if (Application::getInstance()->getSession() === null || $_SERVER['HTTP_HOST'] !== 'misuzu.misaka.nl') { + if ($app->getSession() === null || $_SERVER['HTTP_HOST'] !== 'misuzu.misaka.nl') { http_response_code(403); echo $app->templating->render('errors.403'); exit; diff --git a/public/auth.php b/public/auth.php index 25b29ecb..55c48abc 100644 --- a/public/auth.php +++ b/public/auth.php @@ -103,18 +103,18 @@ switch ($mode) { $session = Session::createSession($user, $user_agent, null, $ipAddress); $app->setSession($session); - set_cookie_m('uid', $session->user_id, 604800); - set_cookie_m('sid', $session->session_key, 604800); + $cookie_life = Carbon::now()->addMonth()->timestamp; + set_cookie_m('uid', $session->user_id, $cookie_life); + set_cookie_m('sid', $session->session_key, $cookie_life); // Temporary key generation for chat login. // Should eventually be replaced with a callback login system. // Also uses different cookies since $httponly is required to be false for these. - $user->last_ip = $ipAddress; $user->user_chat_key = bin2hex(random_bytes(16)); $user->save(); - setcookie('msz_tmp_id', $user->user_id, time() + 604800, '/', '.flashii.net'); - setcookie('msz_tmp_key', $user->user_chat_key, time() + 604800, '/', '.flashii.net'); + setcookie('msz_tmp_id', $user->user_id, $cookie_life, '/', '.flashii.net'); + setcookie('msz_tmp_key', $user->user_chat_key, $cookie_life, '/', '.flashii.net'); header('Location: /'); return; } diff --git a/src/Users/User.php b/src/Users/User.php index 85758072..557c6cff 100644 --- a/src/Users/User.php +++ b/src/Users/User.php @@ -1,6 +1,7 @@ displayRoleInstance === null) { + $this->displayRoleInstance = Role::find($this->display_role); + } + + return $this->displayRoleInstance; + } + public function getDisplayColour(): Colour { - $role = Role::find($this->display_role); + $role = $this->getDisplayRole(); return $role === null ? Colour::none() : $role->role_colour; } + private function getUserTitlePrivate(): string + { + if (!empty($this->user_title)) { + return $this->user_title; + } + + $role = $this->getDisplayRole(); + + if ($role !== null && !empty($role->role_title)) { + return $role->role_title; + } + + return ''; + } + + public function getUserTitle(): string + { + if (empty($this->userTitleValue)) { + $this->userTitleValue = $this->getUserTitlePrivate(); + } + + return $this->userTitleValue; + } + public function addRole(Role $role, bool $setDisplay = false): void { $relation = new UserRole; @@ -159,11 +196,12 @@ class User extends Model public function getDisplayRoleAttribute(?int $value): int { if (!$this->displayRoleValidated) { - if ($value === null || UserRole::where('user_id', $this->user_id)->where('role_id', $value)->count() > 0) { + if ($value === null + || UserRole::where('user_id', $this->user_id)->where('role_id', $value)->count() < 1) { $highestRole = Database::table('roles') ->join('user_roles', 'roles.role_id', '=', 'user_roles.role_id') ->where('user_id', $this->user_id) - ->orderBy('roles.role_hierarchy') + ->orderBy('roles.role_hierarchy', 'desc') ->first(['roles.role_id']); $value = $highestRole->role_id; @@ -184,6 +222,11 @@ class User extends Model } } + public function getLastSeenAttribute(?string $dateTime): Carbon + { + return $dateTime === null ? Carbon::createFromTimestamp(-1) : new Carbon($dateTime); + } + public function getRegisterIpAttribute(string $ipAddress): IPAddress { return IPAddress::fromRaw($ipAddress); diff --git a/utility.php b/utility.php index eb34bd6b..7a02cd75 100644 --- a/utility.php +++ b/utility.php @@ -30,7 +30,7 @@ function set_cookie_m(string $name, string $value, int $expires): void setcookie( "msz_{$name}", $value, - time() + $expires, + $expires, '/', '', !empty($_SERVER['HTTPS']), diff --git a/views/mio/user/view.twig b/views/mio/user/view.twig index 0c305b89..5b382096 100644 --- a/views/mio/user/view.twig +++ b/views/mio/user/view.twig @@ -71,6 +71,12 @@