アクセス許可管理

諏訪子 <suwako@076.moe>、2025-12-29 15:53
コミット 8c76fe27330eb935e14b00e344111fe2ba1295d9 ・親 decf69e ・ファイルを見る

14個のファイル、+126 -11

@@ -0,0 +1,9 @@
1+title: 【新機能】メンバー限定
2+uuid: d49f60c7-6d85-4263-9d3f-c20025f2fa0d
3+author: 諏訪子
4+date: 2025-12-29 06:10:39
5+thumbnail:
6+thumborient: center
7+category: feature,memberonly
8+----
9+これを見れば、ログインしたユーザーという意味です。
@@ -0,0 +1,9 @@
1+title: 【新機能】スタッフ限定
2+uuid: 1c0ac334-a017-4813-9137-939b483c08eb
3+author: 諏訪子
4+date: 2025-12-29 06:13:38
5+thumbnail:
6+thumborient: center
7+category: feature,staffonly
8+----
9+これを見れば、おいらのニッガーという意味です。
変更 newpost.php
@@ -2,6 +2,7 @@
22 if (!isset($argv[1])) die('usage: php newpost.php [slug]');
33 if (file_exists("blog/{$argv[1]}.md")) die("エラー: ファイル「blog/{$argv[1]}.md」は既に存在します。\n");
44
5+define('CURL_ENABLED', false); // 黙れ・・・
56 include('util.php');
67 $post = fopen("blog/{$argv[1]}.md", "w");
78 fwrite($post, "title: 【】\n");
@@ -16,7 +16,7 @@
1616 public function feed(array $params): void {
1717 try {
1818 // 最新の投稿を取得
19- $posts = $this->getPosts('/blog/');
19+ $posts = $this->getPosts('/blog/', null);
2020 // 最新の5件に制限
2121 $posts = array_slice($posts, 0, 5);
2222
@@ -7,9 +7,11 @@
77 *
88 * @return array 投稿の配列
99 */
10- public function getPosts(string $section): array {
10+ public function getPosts(string $section, ?\stdClass $user): array {
1111 $path = ROOT.$section;
1212 $posts = [];
13+ $isMember = $user !== NULL;
14+ $isStaff = $user !== NULL && $user->role === 1;
1315
1416 if (!is_dir($path)) return $posts;
1517 $files = glob($path.'/*.md');
@@ -51,7 +53,7 @@
5153 'thumborient' => $metadata['thumborient'] ?? '',
5254 'category' => $metadata['category'] ?? [],
5355 'uuid' => $metadata['uuid'] ?? '',
54- 'preview' => $preview,
56+ 'preview' => ((!$isMember && in_array('memberonly', $metadata['category'])) || (!$isStaff && in_array('staffonly', $metadata['category'])) ? '未許可' : $preview),
5557 'slug' => $slug,
5658 ];
5759 }
@@ -86,7 +86,7 @@
8686
8787 try {
8888 header('Content-Type: application/activity+json');
89- $posts = $this->getPosts('/blog/');
89+ $posts = $this->getPosts('/blog/', null);
9090 $ap = new Activitypub($posts);
9191 echo $ap->getActivity($uuid);
9292 exit;
@@ -104,7 +104,7 @@
104104 public function apoutbox(array $params): void {
105105 try {
106106 header('Content-Type: application/activity+json');
107- $posts = $this->getPosts('/blog/');
107+ $posts = $this->getPosts('/blog/', null);
108108 $ap = new Activitypub($posts);
109109 echo $ap->getOutbox();
110110 exit;
@@ -34,7 +34,11 @@
3434
3535 $description = 'テクニカル諏訪子ちゃんの個人ブログ';
3636
37- $posts = $this->getPosts('/blog/');
37+ // ユーザー
38+ $auth = new Auth();
39+ $user = $auth->getLoggedInUser();
40+
41+ $posts = $this->getPosts('/blog/', $user);
3842 if (!is_array($posts)) $posts = [];
3943
4044 // 検索機能が使用されている場合
@@ -57,11 +61,7 @@
5761 $postsPerPage
5862 );
5963
60- // ユーザー
61- $auth = new Auth();
62- $user = $auth->getLoggedInUser();
6364 $tmpl->assign('user', $user);
64-
6565 $tmpl->assign('currentPage', $page);
6666 $tmpl->assign('totalPages', $totalPages);
6767 $tmpl->assign('posts', $currentPosts);
@@ -113,6 +113,8 @@
113113 // ユーザー
114114 $auth = new Auth();
115115 $user = $auth->getLoggedInUser();
116+ $isMember = $user !== NULL;
117+ $isStaff = $user !== NULL && $user->role === 1;
116118 $tmpl->assign('user', $user);
117119
118120 $tmpl->assign('pagetit', $pagetit);
@@ -130,10 +132,23 @@
130132 }
131133 }
132134
135+ if (!$isMember && in_array('memberonly', $meta->category)) goto denyaccess;
136+ if (!$isStaff && in_array('staffonly', $meta->category)) goto denyaccess;
137+
138+showpage:
133139 $tmpl->addCss('news-article');
134140 $tmpl->addCss('search');
135141 $tmpl->addCss('blogtype');
136142 $tmpl->render('article');
143+ exit();
144+
145+denyaccess:
146+ unset($md, $meta, $pagetit, $article, $description);
147+ $tmpl->assign('pagetit', 'Not found');
148+ $tmpl->assign('curPage', '404');
149+ $tmpl->assign('menu', $this->getMenu());
150+ $tmpl->assign('description', '');
151+ $tmpl->render('404');
137152 } catch (\Exception $e) {
138153 throw new \Exception($e->getMessage());
139154 }
@@ -32,6 +32,20 @@
3232 'text' => '秘密のページ',
3333 'show' => false,
3434 ],
35+ [
36+ 'class' => 'menu-item',
37+ 'href' => '/memberonly',
38+ 'page' => 'memberonly',
39+ 'text' => 'メンバー限定',
40+ 'show' => true,
41+ ],
42+ [
43+ 'class' => 'menu-item',
44+ 'href' => '/staffonly',
45+ 'page' => 'staffonly',
46+ 'text' => 'スタッフ限定',
47+ 'show' => true,
48+ ],
3549 ];
3650 }
3751 }
@@ -77,4 +77,54 @@
7777 throw new \Exception($e->getMessage());
7878 }
7979 }
80+
81+ public function memberonly(array $params): void {
82+ try {
83+ $tmpl = new Template('/');
84+ $pagetit = 'サインインしたユーザー限定';
85+ $description = 'PHPフレームワークについて';
86+
87+ // ユーザー
88+ $auth = new Auth();
89+ $user = $auth->getLoggedInUser();
90+
91+ $tmpl->assign('user', $user);
92+
93+ $tmpl->assign('pagetit', $pagetit);
94+ $tmpl->assign('curPage', 'memberonly');
95+ $tmpl->assign('custCss', false);
96+ $tmpl->assign('menu', $this->getMenu());
97+ $tmpl->assign('description', $description);
98+
99+ if ($user && $user->role != -1) $tmpl->render('memberonly');
100+ else $tmpl->render('nopermission');
101+ } catch (\Exception $e) {
102+ throw new \Exception($e->getMessage());
103+ }
104+ }
105+
106+ public function staffonly(array $params): void {
107+ try {
108+ $tmpl = new Template('/');
109+ $pagetit = 'スタッフ限定';
110+ $description = 'PHPフレームワークについて';
111+
112+ // ユーザー
113+ $auth = new Auth();
114+ $user = $auth->getLoggedInUser();
115+
116+ $tmpl->assign('user', $user);
117+
118+ $tmpl->assign('pagetit', $pagetit);
119+ $tmpl->assign('curPage', 'staffonly');
120+ $tmpl->assign('custCss', false);
121+ $tmpl->assign('menu', $this->getMenu());
122+ $tmpl->assign('description', $description);
123+
124+ if ($user && $user->role == 1) $tmpl->render('staffonly');
125+ else $tmpl->render('nopermission');
126+ } catch (\Exception $e) {
127+ throw new \Exception($e->getMessage());
128+ }
129+ }
80130 }
@@ -50,7 +50,7 @@
5050
5151 $test->it('アクティビティを受け取れるはず', function($test): void {
5252 $blog = new BlogPost;
53- $posts = $blog->getPosts('/blog/');
53+ $posts = $blog->getPosts('/blog/', null);
5454 $ap = new Activitypub($posts);
5555 $res = $ap->getActivity('a8c04518-4181-4ec6-9ef0-3f88f23b84b6'); // /blog/feature-test
5656 $test->assertNotNull($res);
@@ -0,0 +1,6 @@
1+{@ include(common/header) @}
2+ <h1 class="paragraph">メンバー限定</h1>
3+ <p class="paragraph">
4+ いらっしゃいませ、ご主人様! ^^) _旦~~
5+ </p>
6+{@ include(common/footer) @}
@@ -0,0 +1,3 @@
1+{@ include(common/header) @}
2+ <p>表示許可却下。失礼。</p>
3+{@ include(common/footer) @}
@@ -0,0 +1,6 @@
1+{@ include(common/header) @}
2+ <h1 class="paragraph">スタッフ限定</h1>
3+ <p class="paragraph">
4+ よろしくね、社員。(´・ω・`)
5+ </p>
6+{@ include(common/footer) @}