I am developing a site in cakephp2.5. I have two plugin Webmaster and debugKit. When I write
CakePlugin::load('Webmaster', array('bootstrap' => false, 'routes' => false));
CakePlugin::load('webmaster');
CakePlugin::load( 'DebugKit');
The site works correctly on local system but not on live server. However if I remove one of the above Webmaster it shows error on local system and also on live
Error: The application is trying to load a file from the webmaster plugin
Error: Make sure your plugin webmaster is in the app\Plugin directory and was loaded
I also tried but no luck. Struggling from 2 days. Also seen these links
link1
link2
Here is my WebmasterAppController
<?php
App::uses('AppController', 'Controller');
class WebmasterAppController extends AppController{
public $theme= "beyond";
//public $layout=NULL;
public function beforeFilter(){
//$this->Auth->allow('login');
$this->Auth->loginAction= array('controller'=>'users', 'action'=>'login');
$this->Auth->loginRedirect= array('controller'=>'users', 'action'=>'index');
$this->Auth->loginError= 'Invalid Email/Password!';
$this->Auth->authError= 'You are not authorised to access!';
$this->Auth->logoutRedirect= array('controller'=>'users', 'action'=>'login');
AuthComponent::$sessionKey = 'Auth.Webmaster';
//we don't need to load debug kit
$this->Components->unload('DebugKit.Toolbar');
parent::beforeFilter();
}
}
And here is AppController
class AppController extends Controller{
public $cakeDescription = "CakePhp | ";
public $theme = "alpus";
public $ext = 'html';
public $helpers = array('NiceForms', 'coolFun');
public $components = array(
'DebugKit.Toolbar',
'Common',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'dashboard'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login',
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields' => array('username' => 'email')
)
),
'sessionKey' => 'Admin'
)
);
public function beforeFilter(){
if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
$this->theme = 'smart';
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'admin_login', 'plugin' => false);
}
$this->Auth->allow('register');
}
}
Edit 1:
For cakephp 3.0 we can use
public function beforeFilter(Event $event) {
$this->Auth->sessionKey ='Auth.User';
}
in AppController.php to set different sessionKey for frontend and backend.
Why do you have multiple load calls for the same plugin anways? There should be only one per plugin!
That being said, mind your casing, the second CakePlugin::load() call uses webmaster instead of Webmaster. Plugin names should start with an uppercased letter, just like the corresponding directory name.
Your local filesystem is most probably case insensitive, so it can find the plugin directory even though the casing doesn't match.
Update It looks like I intially got you wrong, if CakePHP would tell you to load the plugin webmaster without you already having added a CakePlugin::load('webmaster') call, then you must have used the lowercased webmaster somewhere else in your code.
Related
I have created a little project with an admin section. I am using admin routes to redirect to admin actions in my controllers. The website has pages that are available to everyone with no login required. To access the /admin or /admin/users, etc... You must login.
I have spread my admin actions across my controllers like "admin_login", "admin_users", ...
So my question is, when someone goes to /admin/users or some other adminpage, I have to check in each controller action if the user is in the session and otherwise redirect to thelogin form.
Is there a way to do this in one place? I used a beforefilter in my AppController class.
When using something like this, I get an infinite loop:
AppController.php
class AppController extends Controller {
public $helpers = array('Paginator','Acl.AclHtml');
public $components = array('Acl', 'Session',
'Auth' => array(
'authError' => 'You are not authorized to access that location.',
'authorize' => array(
'Actions' => array(
'actionPath' => 'controllers')
),
'controllers' => array('users')
));
public function beforeFilter() {
if(isset($this->request->prefix) && ($this->request->prefix == 'admin')){
$username = $this->Session->read('Admin.username');
if (empty($username)) {
$this->redirect (array(
'controller'=>'users',
'action'=>'login',
'admin'=>true
));
} else {
$this->redirect (array(
'controller'=>'admin',
'action'=>'dashboard',
'admin'=>true
));
}
}
// LDAP
$server_ip = $_SERVER['SERVER_ADDR'];
$ldapIp = ClassRegistry::init('LdapIp');
$ldapIpCount = $ldapIp->find('count', array('conditions' => array('ldap_ip' => $server_ip)));
if ($ldapIpCount >= 1) {
$this->Auth->authenticate = array('Ldap');
} else {
$this->Auth->authenticate = array('Form');
}
$this->Auth->authenticate = array('Form');
$this->Auth->allow();
if (!$this->Auth->isAllow($this)) {
$this->set(array(
'message' => array(
'text' => __('un aunthaticated request'),
'type' => 'error',
'status' => "401"
),
'_serialize' => array('message')
));
throw new ForbiddenException();
}
}
}
The front login with LDAP (Active directory).
UsersController.php
App::uses('AppController', 'Controller');
App::uses('Sanitize', 'Utility');
class UsersController extends AppController {
public $components = array('Paginator', 'Session', 'RequestHandler', 'Auth', 'Acl');
public function admin_login() {
$this->layout = 'admin_login';
if ($this->request->is('post')) {
$username = $this->request->data['User']['username'];
$password = $this->request->data['User']['password'];
$password = Security::hash($password, null, true);
$logged_in = $this->User->find('count', array('conditions' => array('User.username' => $username, 'User.password' => $password, 'User.role' => 'Admin', 'User.active' => 1)));
if ($logged_in >= 1) {
$this->Session->setFlash(__('Login successful!'), 'default', array('class' => 'alert alert-success'));
$users = $this->User->find('first', array('conditions' => array('User.username' => $username, 'User.password' => $password, 'User.role' => 'Admin', 'User.active' => 1)));
$this->Session->write('Admin.id', $users['User']['id']);
$this->Session->write('Admin.username', $users['User']['username']);
$this->Session->write('Admin.group_id', $users['User']['group_id']);
$this->Session->write('Admin.full_name', $users['UserProfile']['fname'] . " " . $users['UserProfile']['lname']);
$this->redirect(array('controller' => 'admin', 'action' => 'dashboard', 'admin' => true));
} else {
$this->Session->setFlash(__('Username or password is incorrect!'), 'default', array('class' => 'alert alert-error'));
}
}
}
public function admin_logout() {
$this->Session->delete("Admin");
//$this->Session->destroy();
$this->Session->setFlash(__('Logged out successful!'), 'default', array('class' => 'alert alert-success'));
$this->redirect(array('controller' => 'users', 'action' => 'login', 'admin' => true));
}
}
Yoy are getting an infinite loop because beforeFilter() will be called when you attempt to access /admin/users/login.
The proper way of dealing with your needs is setting up the Auth Component.
Once you've set up component, in UsersController::beforeFilter() you have to allow access to those actions that don't require login by means of the allow() method. E.g.
public function beforeFilter() {
$this->Auth->allow(array('signup'));
parent::beforeFilter();
}
This is also applicable to any other controller with actions that need to be accessed by non logged in users.
The loginAction you define in the Auth component configuration will be automatically allowed access.
In the blog tutorial you will find a good example of the Auth component usage.
Edit
As mentioned, AppController::beforeFilter() is always called, even when you try to access /admin/users/login. To prevent this from happening, try adding the following condition:
if (empty($username) && $this->action!='login') {
$this->redirect (array(
'controller'=>'users',
'action'=>'login',
'admin'=>true
));
}
You wouldn't need this if you allowed AuthComponent to take care of authentication for you.
Still, there's no guarantee that your code will work as expected. You are making your life difficult by not using AuthComponent to its fullest. I recommend that you research on the topic:
Creating Custom Authorize objects in the Cookbook 2.x
LdapAuth in cakephp 2.0 in Stack Overflow
I just started learning about the Auth component and I'm having a problem with redirection. The path of my local aplication is: localhost/school but when a logged user tries to acces to a url he isnt't allowed the site redirects to localhost/school/school and it says "The requested address '/school/school/' was not found on this server". I want no redirection when this happens, just show "you are not allowed" in the same page or maybe redirect to specific error page, how can I do that?. I have no problems with login or logout redirection, only what I said before. This is my App Controller:
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
),
'Session'
);
public $helpers = array('Html', 'Form', 'Session');
public function beforeFilter() {
//Configure AuthComponent
$this->Auth->loginAction = array(
'controller' => 'users',
'action' => 'login'
);
$this->Auth->logoutRedirect = array(
'controller' => 'users',
'action' => 'login'
);
$this->set('current_user',$this->Auth->User());
$this->Auth->authError = "You're not allowed.";
}
I had the same problem and I solved it.
Try this code in AppController
public function beforeFilter() {
//Configure AuthComponent
// note just these two lines
$this->Auth->unauthorizedRedirect=FALSE ;
$this->Auth->authError="Access Denied";
$this->Auth->loginAction = array(
'controller' => 'users',
'action' => 'login'
);
$this->Auth->logoutRedirect = array(
'controller' => 'users',
'action' => 'login'
);
$this->Auth->loginRedirect = array(
'controller' => 'posts',
'action' => 'add'
);
$this->Auth->allow('display');
//$this->Auth->allow();
}
class AppController extends Controller {
// added the debug toolkit
// sessions support
// authorization for login and logut redirect
public $components = array(
'Session','Flash',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authError' => 'You must be logged in to view this page.',
'loginError' => 'Invalid Username or Password entered, please try again.'
));
// only allow the login controllers only
public function beforeFilter() {
$this->Auth->allow('login');
}
public function isAuthorized($user) {
// Here is where we should verify the role and give access based on role
return true;
}
}
and in your controller it should be like this :
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login','add');
}
public function login() {
//if already logged-in, redirect
if($this->Session->check('Auth.User')){
$this->redirect(array('action' => 'index'));
}
// if we get the post information, try to authenticate
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->Flash->set(__('Welcome, '. $this->Auth->user('username')));
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->set(__('Invalid username or password'));
}
}
}
If you're not allowing someone access to a page, then what do you want the controller to do when they request it?
For example, you can set a redirect with :
$this->redirect(array(
'controller'=>'users',
'action' => 'login'));`
You can display a message using Session::setFlash();
localhost/projectName/projectName is a redirection when you don't have permission to this action. I had same problem. I comment for a moment 'Actions' => array('actionPath' => 'controllers') ) in $components. After that I set aros_acos by executing this code:
$group = $this->User->Group->read(null,'1');
$this->Acl->allow($group, 'controllers/Users/controlPanel');
After that I uncomment code, and in action 'controlPanel' and error disappear :) I don't know how I can change this redirection, but if I have record in aros_acos everything works.
Here is code of appcontroller
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'employees',
'action' => 'login'
),
'logoutRedirect' => array(
'controller' => 'employees',
'action' => 'logout',
'index'
)
)
);
public function beforeFilter() {
$this->Auth->allow('index','add');
}
ERROR
usercontroller not found //why it is coming
my model class name is Employee
Controller name id employeesController
please help me on this as i am new to cake php
Adding Auth to your used components suggests you are logging users in. You can read more about how it works here
You can take out the Auth out and try again, or set up the user authentication.
How does your EmployeesController look like?
I'm getting issue after logging in the site. There are two kinds of users i.e. 'admin','employer'. When I've logged in by employer, I can access the restricted area of Admin. Below is the AppController of the site..
class AppController extends Controller {
public $helpers = array('Form', 'Html', 'Js', 'Time', 'Auth');
// Change template extension to .php instead of .ctp
var $ext = '.php';
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array(
'controller' => 'users',
'action' => 'login'
),
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'authenticate' => array('Form' => array('fields' => array('username' => 'email'))),
'authorize' => array('Controller')
)
);
public function isAuthorized($user) {
// Admin can access every action
if (isset($user['type']) && $user['type'] === 'admin') {
return true;
}
// Default deny
return false;
}
public function beforeFilter() {
$this->Auth->allow(array('view', 'index','assessment','question'));
}
}
Now here is the controller which has methods for admin.
class TopicsController extends AppController {
public $scaffold = 'admin';
public function beforeFilter() {
if($this->Auth->user('type')!='employer'){
parent::beforeFilter();
$this->Auth->allow(array('view', 'index','moveup'));
} else {
$this->Auth->deny(array('view', 'index','moveup'));
$this->redirect(array('controller' => 'employer' , 'action' => 'index'));
}
}
public function isAuthorized($user) {
return true;
}
public function index() {
$this->set('topics', $this->Topic->children());
}
}
If admin URL is www.example.com/admin/topics , Employer is redirected to www.example.com/admin/employer which is not right URL to be redirected.
Also want to know about public $scaffold = 'admin'; as It's little unclear to me.
Please help me..
Ok.. Found one way to redirect, which made my issue solved for a now.. Still looking for proper answer if anybody has..
I changed code from
$this->redirect(array('controller' => 'employer' , 'action' => 'index'));
to
$this->redirect('employer');
..
EDIT: Thanks Alex, I've used
$this->redirect(array('controller' => 'employer' , 'action' => 'index', 'admin'=>false));
and it's working too..
Hi all I'm currently using cake 2.1, I am trying to get a page to render as a pdf in the browser however I believe I'm not loading the engine correctly. I believe it is because I'm not loading anything in routes.php.
here is the relevant code in routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
Router::mapResources(array('Invoices'));
/**
* Load the CakePHP default routes. Remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
here is boostrap.php
<?php CakePlugin::load('DebugKit');
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
CakePlugin::loadAll();
here is the function in the controller
public function view($id = null) {
$this->set('title_for_layout', 'Invoices');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.png');
$this->layout='adminpdf';
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'download'=>true,
'binary'=>'C:\Program Files (x86)\wkhtmltopdf\wkhtmltopdf.exe'));
$this->pdfConfig = array('engine' => 'CakePdf.WkHtmlToPdf');
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) {
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'potrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->Invoice->read(null, $id));
//Retrieve Account Id of current User
$accountid=$this->Auth->user('account_id');
//Find all Invoices where $conditions are satisfied
$invoicedetails=$this->Invoice->find('first', array(
'conditions' => array('Invoice.id'=>$id)));
//prints fieldsInvoice details, including invoice and field information
$invoices=$this->FieldsInvoice->find('all',array(
'conditions'=>array(
'invoice_id'=>$id)));
$itemInvoice=$this->InvoicesItem->find('all',array('conditions'=>array('invoice_id'=>$id)));
//Set variables
$this->set('invoicedetails', $invoicedetails);
$this->set('invoice', $invoices);
$this->set('accountid', $accountid);
$this->set('itemInvoice', $itemInvoice);
}
I am using this method of loading the pdf - https://github.com/ceeram/CakePdf/
and am using this engine wkhtmltopdf
I have been stuck on this for several days so any help would be greatly appreciated.
you use WkHtmlToPdf you need to download that engine because its not default.
check here : app/plugin/cakepdf/Vendor.
you see default : dompdf , mpdf and tcpdf.
i use DomPdf.
in your bootstrap.php load the engine like this :
Configure::write('CakePdf', array(
'engine' => 'CakePdf.DomPdf',
'pageSize'=>'A4',
'orientation' => 'landscape',
));
and use inside your controller :
$this -> pdfConfig = array (
'orientation' => 'landscape',
'download' => true,
'filename' => 'Invoice_' . $id
);
you can also take a look at : http://www.slideshare.net/jellehenkens/building-php-documents-with-cakepdf-cakefest-2012
good luck i hope this will help.