S2OJ/web/app/models/UOJUserBlog.php

51 lines
1.3 KiB
PHP
Raw Normal View History

2022-11-06 02:26:21 +00:00
<?php
class UOJUserBlog {
public static ?array $user = null;
public static function init() {
$username = blog_name_decode($_GET['blog_username']);
if (!validateUsername($username) || !(self::$user = UOJUser::query($username))) {
UOJResponse::page404();
}
if ($_GET['blog_username'] !== blog_name_encode(self::$user['username'])) {
permanentlyRedirectTo(HTML::blog_url(self::$user['username'], '/'));
}
}
public static function check() {
return self::$user !== null;
}
public static function id() {
if (self::$user === null) {
return null;
}
return self::$user['username'];
}
public static function user() {
return self::$user;
}
2022-11-11 23:10:34 +00:00
public static function userIsOwner(?array $user) {
if ($user === null) {
return false;
}
return self::$user['username'] === $user['username'];
}
2023-02-07 10:38:24 +00:00
public static function userHasManagePermission(?array $user) {
if ($user === null) {
return false;
}
return isSuperUser($user) || UOJUser::checkPermission($user, 'blogs.manage');
}
2022-11-06 02:26:21 +00:00
public static function userCanManage(?array $user, ?string $whose_blog = null) {
if ($whose_blog === null) {
$whose_blog = self::id();
}
2022-11-11 23:10:34 +00:00
return $user && (isSuperUser($user) || UOJUser::checkPermission($user, 'blogs.manage') || $user['username'] === $whose_blog);
2022-11-06 02:26:21 +00:00
}
}