現在、Auth コンポーネントを使用して、ログインしていないユーザーのアプリケーションへのアクセスをロックしています。ただし、ユーザーが「登録」できるように、users_controller の追加アクションがルーティングで利用可能になっています。(要点は、誰でもアプリの機能にアクセスするために登録できるということですが、一意の管理者ユーザーを実装する必要があります)
Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
したがって、「登録」リンクがログインフォームとともに views/pages/home.ctp に配置されています。
<p>
Only takes about 1 minute to <?php echo $html->link('register', '/register'); ?> and its free.
</p>
<div id="login">
<h2>Login</h2>
<?php
if ($session->check('Message.auth')) $session->flash('auth');
echo $form->create('User', array('action' => 'login'));
echo $form->input('username', array('value' => 'billy'));
echo $form->input('password', array('value' => 'billy'));
echo $form->end('Login');
?>
</div>
ビュー/要素/header.ctp:
<?php if($html->loggedIn()): ?>
<ul class="right">
<li><?php echo $html->link('Log Out', '/users/logout'); ?></li>
</ul>
</div>
<?php endif; ?>
<!-- end nav -->
私の大きな問題は、管理者だけがユーザーを編集および削除できるように、できれば users_controller の編集アクションと削除アクションを分離する ACL なしで、簡単な方法を見つける必要があることです。
users テーブルのデータベース構造には、一意のユーザー名 (VARCHAR) と一意の ID (INT) が必要です。
ユーザー名に入力された文字列が「admin」であるか、「id」の値が 1 に等しいかどうかを示すチェックをログイン フォームに実装する簡単な方法はありますか?この一意の「admin」ユーザー アクセスのみを許可します。ユーザーコントローラーの編集および削除アクション? いくつかのルーティングが関与している可能性があると思います。
または、コアで cake_admin ユーザーを有効にすることは実行可能なオプションですか?
リクエストに応じて、より多くのコードを利用できるようにすることができます。バージョン1.2で作業していることを覚えておいてください
users_controller.php:
function edit($id = null)
{
if(!$this->Session->read('Auth.User.admin') == 1) {
// Not an admin user, go back where we came from
$this->redirect($this->referer());
}
$this->idEmpty($id, 'index');
if (!empty($this->data))
{
if ($this->User->save($this->data))
{
$this->flashSuccess('The User has been saved', 'index');
}
else
{
$this->flashWarning('The User could not be saved. Please, try again.');
}
}
if (empty($this->data))
{
$this->data = $this->User->read(null, $id);
}
}
function delete($id = null)
{
if(!$this->Session->read('Auth.User.admin') == 1) {
// Not an admin user, go back where we came from
$this->redirect($this->referer());
}
$this->idEmpty($id, 'index');
if ($this->User->del($id))
{
$this->flashSuccess('User Deleted', 'index');
}
}