- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在用 cakephp 3 制作一个应用程序,我的用户有两个角色,管理员和学生。管理员可以访问所有内容,而学生只能访问他的个人资料。我已经完成了这部分,但我不知道如何限制学生只看他的个人资料而不是所有个人资料。例如,如果我用用户 3 登录,这是 url http://localhost:8765/users/view/4查看用户 4 的配置文件,但如果将 4 更改为 3,我可以看到用户 3 的配置文件。我该如何解决这个问题?
AppController.php
class AppController extends Controller
{
public function initialize()
{
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'loginRedirect' => [
'controller' => 'Users',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'login'
]
]);
}
public function beforeFilter(Event $event)
{
$this->Auth->allow(['login']);
}
public function isAuthorized($user)
{
// Admin can access every action
if (isset($user['rol']) && $user['rol'] === 'admin') {
return true;
}
// Default deny
return false;
}
}
UserController.php
class UsersController extends AppController
{
/**
* Index method
*
* @return void
*/
public function index()
{
$this->paginate = [
'contain' => ['Grados']
];
$this->set('users', $this->paginate($this->Users));
$this->set('_serialize', ['users']);
}
/**
* View method
*
* @param string|null $id User id.
* @return void
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function view($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Grados', 'Clases', 'ConveniosUsuarios', 'Desvinculaciones', 'HistorialAlumnos', 'Pagos', 'Pedidos']
]);
$this->set('user', $user);
$this->set('_serialize', ['user']);
}
/**
* Add method
*
* @return void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$grados = $this->Users->Grados->find('list', ['limit' => 200]);
$this->set(compact('user', 'grados'));
$this->set('_serialize', ['user']);
}
/**
* Edit method
*
* @param string|null $id User id.
* @return void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
$filename = WWW_ROOT.'files'.DS.'images'.DS.$this->request->data['id'].$this->request->data['foto']['name'];
move_uploaded_file($this->request->data['foto']['tmp_name'],$filename);
$user->set('foto',$filename);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$grados = $this->Users->Grados->find('list', ['limit' => 200]);
$this->set(compact('user', 'grados'));
$this->set('_serialize', ['user']);
}
/**
* Delete method
*
* @param string|null $id User id.
* @return void Redirects to index.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$user = $this->Users->get($id);
if ($this->Users->delete($user)) {
$this->Flash->success(__('The user has been deleted.'));
} else {
$this->Flash->error(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
// Allow users to register and logout.
// You should not add the "login" action to allow list. Doing so would
// cause problems with normal functioning of AuthComponent.
$this->Auth->allow(['logout']);
}
public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
if ($this->Auth->user('rol') == 'Alumno') {
$this->redirect('users'.DS.'view'.DS.$this->Auth->user('id'));
}else{
return $this->redirect($this->Auth->redirectUrl());
}
}else{
$this->Flash->error(__('Usario o contraseña invalidos!'));
}
}
}
public function logout()
{
return $this->redirect($this->Auth->logout());
}
public function isAuthorized($user)
{
$userid=$this->Auth->user('id');
$action = $this->request->params['action'];
if ($user['rol']=='Admin') {
return true;
}else if ($user['rol']!='Admin') {
if (in_array($action, ['edit', 'view'])) {
return true;
}
return false;
}
return parent::isAuthorized($user);
}
}
调试($this->请求)
object(Cake\Network\Request) {
params => [
'plugin' => null,
'controller' => 'Users',
'action' => 'view',
'_ext' => null,
'pass' => [
(int) 0 => '4'
]
]
data => []
query => []
cookies => [
'CAKEPHP' => 't8o6augt5qd0a8p3squq4kmni2'
]
url => 'users/view/4'
base => ''
webroot => '/'
here => '/users/view/4'
trustProxy => false
[protected] _environment => [
'DOCUMENT_ROOT' => 'C:\xampp\htdocs\intranet\webroot',
'REMOTE_ADDR' => '::1',
'REMOTE_PORT' => '50389',
'SERVER_SOFTWARE' => 'PHP 5.6.8 Development Server',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'SERVER_NAME' => 'localhost',
'SERVER_PORT' => '8765',
'REQUEST_URI' => '/users/view/4',
'REQUEST_METHOD' => 'GET',
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => 'C:\xampp\htdocs\intranet\webroot\index.php',
'PATH_INFO' => '/users/view/4',
'PHP_SELF' => '/index.php',
'HTTP_HOST' => 'localhost:8765',
'HTTP_CONNECTION' => 'keep-alive',
'HTTP_CACHE_CONTROL' => 'max-age=0',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36',
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, sdch',
'HTTP_ACCEPT_LANGUAGE' => 'es-ES,es;q=0.8,en;q=0.6',
'HTTP_COOKIE' => 'CAKEPHP=t8o6augt5qd0a8p3squq4kmni2',
'REQUEST_TIME_FLOAT' => (float) 1437761676.7461,
'REQUEST_TIME' => (int) 1437761676,
'HTTP_X_HTTP_METHOD_OVERRIDE' => null,
'ORIGINAL_REQUEST_METHOD' => 'GET',
'HTTPS' => false
]
[protected] _detectors => [
'get' => [
'env' => 'REQUEST_METHOD',
'value' => 'GET'
],
'post' => [
'env' => 'REQUEST_METHOD',
'value' => 'POST'
],
'put' => [
'env' => 'REQUEST_METHOD',
'value' => 'PUT'
],
'patch' => [
'env' => 'REQUEST_METHOD',
'value' => 'PATCH'
],
'delete' => [
'env' => 'REQUEST_METHOD',
'value' => 'DELETE'
],
'head' => [
'env' => 'REQUEST_METHOD',
'value' => 'HEAD'
],
'options' => [
'env' => 'REQUEST_METHOD',
'value' => 'OPTIONS'
],
'ssl' => [
'env' => 'HTTPS',
'options' => [
(int) 0 => (int) 1,
(int) 1 => 'on'
]
],
'ajax' => [
'env' => 'HTTP_X_REQUESTED_WITH',
'value' => 'XMLHttpRequest'
],
'flash' => [
'env' => 'HTTP_USER_AGENT',
'pattern' => '/^(Shockwave|Adobe) Flash/'
],
'requested' => [
'param' => 'requested',
'value' => (int) 1
],
'json' => [
'accept' => [
(int) 0 => 'application/json'
],
'param' => '_ext',
'value' => 'json'
],
'xml' => [
'accept' => [
(int) 0 => 'application/xml',
(int) 1 => 'text/xml'
],
'param' => '_ext',
'value' => 'xml'
],
'mobile' => object(Closure) {
},
'tablet' => object(Closure) {
}
]
[protected] _detectorCache => []
[protected] _input => ''
[protected] _session => object(Cake\Network\Session) {
[protected] _engine => null
[protected] _started => true
[protected] _lifetime => '1440'
[protected] _isCLI => false
}
}
最佳答案
// UsersController.php
public function isAuthorized($user) {
if (in_array($this->request->action, ['edit', 'delete'])) {
$id = (int) $this->request->params['pass'][0];
if ($id == $user['id']) {
return true;
}
}
return parent::isAuthorized($user);
}
// AppController.php
public function isAuthorized($user)
{
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
return false;
}
关于php - cakephp 3 允许用户只编辑他们的个人资料,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31502638/
经验丰富的程序员,但对 CakePHP 2.1 不熟悉,我花了一天的时间努力让自定义 Helper 在 View 中工作,遵循手册:http://book.cakephp.org/2.0/en/vie
任何人都可以列出 CakePHP 框架相对于其他 PHP 框架的缺点吗? 1) 请解释为什么缺少的功能对 PHP 项目如此重要? 2) 该缺失的功能如何帮助那些使用具有该功能的框架的 Web 开发人员
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
我对 PHP 比较陌生。开始学习 PHP,但后来遇到了 cakePHP,它可以加快开发时间。 阅读文档和博客教程后,我仍然不明白命名约定。我想在我开始做一些例子之前我不会知道,但是为了让我开始,有人可
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
任何人都可以告诉我为什么当一个字段没有翻译时它返回空,而不是默认翻译? 我会感谢所有建议... 不知道你想看我代码的哪一部分,因为它都是直接来自 www.book.cake.org。所以我粘贴了一些代
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在编写一个支持多种测量单位的应用程序。在极少数情况下,用户想要更改他们的测量系统,我需要运行一个查询,该查询应用乘数将应用程序中的每个单位列缩放到正确的测量系统。为了确保所有数据在此操作出现问题时
很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center . 8年前关闭
在用户模型中: var $hasMany = array( 'Photo' => array( 'className' => 'Photo',
剧透:我想更好地了解对 CakePHP 应用程序的请求的生命周期。 背景:我使用的是 CakePHP v2.3。我正在调试一个问题,该问题导致我无法找到的不想要的重定向。我正在尝试通过增量添加 die
在我的 Cake 应用程序中,我有一个 Controller “completed_projects”。它的索引操作不带参数并列出了一些项目。可以访问不同的页面example.com/complete
我找不到有关cakeError()定义为成员函数的任何信息。 documentation仅声明该调用看起来像这样: $this->cakeError(string $errorType [, arra
我创建了一个自定义数据源,该数据源从Web api获取数据,现在我正在研究实现错误处理。 在数据源中,我正在调用$ model-> onError()。在模型中,我创建了onError方法,并且可以使
除了重定向和渲染之外,还有其他命令可以将 Controller 重定向到特定 View 页面吗? 我的重定向为 $this->redirect('/forms/homepage/'.$userId);
我试图在我的字段中查找计数并连接该计数,但它给出错误消息,因为找不到 make_count 并且我在上面创建了相同的字段。 $this->Car->virtualFields['make_count'
我想要条件验证,即在更新密码时隐藏字段,并且密码验证为“6 到 15”个字符!因此密码以哈希格式存储在数据库中,因此不允许我更新。我的模型代码如下, array( 'rule
echo $form->input( 'country_id', array( 'type' => 'select', 'label' => __('C
我正在使用 CakePHP 创建一个锦标赛平台。目前,我已经创建了以下可以完美运行的表格、模型和 Controller :锦标赛、用户和团队。 我还创建了一个“Tournamentuser”表、 Co
我的场景是这样的: 在我的/views/layout/default.ctp 在我的/views/pages/home.ctp Html-
我是一名优秀的程序员,十分优秀!