Is there an Auth Component's helper in Cakephp 2.x?
Currently I just pass the $Auth object to the view in the AppController like so:
$this->set('Auth', $this->Auth);
I searched around but there doesnt seem to be a helper available by default. I need some of the functions of the Auth component in the views like Auth::loggedIn().
help?
There is no need for an AuthHelper
The AuthComponent::user function can be called statically:
if (AuthComponent::user()) {
// user is logged in
}
Or since it just reads from the session the same information can also be found via the session (component/helper/class):
if ($this->Session->read('Auth.User')) {
// user is logged in
}
It is not a good idea, or required to pass the Auth component (or any component) to the view.
The Auth Component still exists: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
Related
I am using the Laravel 5.2 Auth system coming up with this command :
php artisan make:auth
Although this works totally fine as is, my goal is to use an external API to perform login, registering and changing password while still being able to use the core function of the Auth class.
So taking the login for example, I want to use something like
function login(ApiController $api) {
// This function return data or error code and message in JSON
$login = $api->login([ $credentials['email'], $credentials['password']]);
if($login->success)
// login successfully like normal Auth would do
else
// redirect to main page with $login->message
}
By the way, I want to pass fields coming up from $login to the Auth class, like we can actually do Auth::user()->email giving us the email, I'd want to set value like "database field" but with my API JSON fields behind
I looked on the Internet and found something to do inside AuthController and something related to ServiceProvider, but I don't know how to follow my exact needs
Adding a custom user provider would help in this case. There is no need to play with AuthController here. Check this Laravel Docs page.
You will need to create a new User Provider which implements Illuminate\Contracts\Auth\UserProvider, specify it in AuthServiceProvider and update your auth config file accordingly.
Here are the links to the framework's default User Providers for reference :
1) DatabaseUserProvider
2) EloquentUserProvider
I ended up coding my own Auth system...
Using session() and Input()
In Zend Framework whats a better method to restrict user from accessing post login pages
if the user is not logged in.Without Zend I would have write a code to check if the
userid is set in session or not and put that code in common file(being used on all post
login pages).
In Zend Framework I can check that using hasIdentity method of zend_auth but I don't
want to check that inside all actions.The another method I am thinking is to create a
plugin and hook it to routeshutdown event,then after fetching controller and action
name from request object I can decide whether to redirect user or not.
Please suggest some good method to implement it in zend framework
You would normally write a Controller Plugin and add it to the front controller. This way you can intercept the dispatch and route process where you want and issue a redirect to an appropriate page.
class Your_Controller_Plugin_Authcheck extends Zend_Controller_Plugin_Abstract
{
}
In the Bootstrap class:
protected function _initFrontControllerPlugins()
{
$this->bootstrap(array('AuthcheckPlugin', 'FrontController'));
$front = $this->getResource('FrontController');
$front->registerPlugin($this->getResource('AuthcheckPlugin'));
}
protected function _initAuthcheckPlugin()
{
$plugin = new Your_Controller_Plugin_Authcheck();
return $plugin;
}
BTW: This is Zend Framework 1 - might work the same in 2, but I believe you might change things a bit there if you already use it.
In everything CakePHP app I have worked with recently, the login function is empty so how on earth does the login functionality work?? I presume somehow Cake is defaulting, not sure how it knows to even default it, but where is/are these defaults?
i.e function login() {}
look at the auth component https://github.com/cakephp/cakephp/blob/master/cake/libs/controller/components/auth.php#L680
The whole magic happens in the startup() callback of the AuthComponent. That method is triggered before the controller action is executed. It checks if there is POST data in the defined format (data[UserModel][usernameField], etc.), validates it against the User model and redirects you to loginRedirect, if it was successful.
CakePHP knows on which controller/action pair to act through the $loginAction property you can set to the AuthComponent.
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
i hope this tutorial will be useful for user login management
I have an edit action in the users controller. What I want to do is redirect anyone to a different action if their Auth.User.id does not equal the id of the user they are trying to edit.
I can access variables in my views like this:
if($session->read('Auth.User.id') != $id){
but this doesn't work in my controller. Getting:
Undefined variable: session
How do I access session data within a controller? also, if any has a better way of achieving what I want to do, feel free to add!
Thanks,
Jonesy
You must first add Session as a component in your controller:
var $components= array('Session');
You can then access it in your methods via $this->Session
You can read Session data in a controller with $this->Session->read('Auth.User.id'); The CakePHP Session component, if I remember correctly, is automatically loaded into all controllers unless you have defined the default components elsewhere. If $this->Session is undefined, include it into your $components array in your controller like var $components = array('Session');
It's important to note that Helpers are not the same as Components. Generally speaking, Components are extended functionality for your Controller. Whereas Helpers are extended functionality for your view.
For a complete look at all possible methods, the CakePHP Cookbook will be invaluable for you! http://book.cakephp.org/view/1310/Sessions
i'm trying to work out how i can stop zend or redirect zend to go to a different zend controller and action if a check within the boot strap fails.
for example a get variable does not exist or more likely a session does not exist meaning the user must log in.
so the user had originally requested index/someaction
but i want them to go to login/index
within my boot strap i would place the condition and then change the controller action to view.
if i'm doing this in a way that is not standard can anyone direct me to documentation on the best practice ?
zend novice
From Zend documentation (Dispatcher)
// forward to an action in another controller:
// FooController::bazAction(),
// in the current module:
$this->_forward('baz', 'foo', null, array('baz' => 'bogus'));
I'd sugest you to do with plugins for access check on each page and for login create an authentication controller.
Here you'll find out how to do this http://alex-tech-adventures.com/development/zend-framework/61-zendauth-and-zendform.html
An example:
class Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
// ...
if(!$auth->hasIdentity())
{
$request->setControllerName('authentication')
->setActionName('login');
}
}
}
I don't usually put authentication in the bootstrap, that should have it's own controller.
Create an AuthController() to set up your auth adapter and and set up your instance.
Then in a common view (for secure pages), just check your instance with something like:
$auth = Zend_Auth::getInstance();
if(!$auth->hasIdentity())
{
#re-direct to login page
}