I want to use AuthComponent in CakePHP with a model (and its corresponding table) named Admin ('admins' table in the database). I've used $this->Auth->userModel = 'Admin'; as the documenation says in the BeforeFilter() function of the app_controller, to indicate that I'm not using the deafult model User.
However, either I'm doing it in the wrong place or there's something else that needs to be done, because I can't make the authentication to work (the login function in my admins_controller.php). The rest of functions work well (for example the add function).
If I use the name User for my model with a users table, it works perfectly. How do I fix this problem?
Make sure you call parent::beforeFilter() in your AdminController::beforeFilter().
Related
I'm developing a CakePHP 3 application.
Now, i need to encrypt some data with SHA1 before the save of the entity.
I tried the beforeSave() callback in Table Object, like in CakePHP 2.x, but it doesn't work.
So, i discovered that type of change (update data on beforeSave/beforeUpdate) in current version, needs to be adapted to accessors & mutators, like the docs says (http://book.cakephp.org/3.0/en/orm/entities.html#accessors-mutators).
Documentation even has a note about check if an entity field was modified (http://book.cakephp.org/3.0/en/orm/entities.html#checking-if-an-entity-has-been-modified) but i dont understand how to use this.
I need some simple logic, just like an authentication system.
Before Save, in User Model, the field responsible_card_password must be hashed with SHA1 if this is filled. If isnt filled, the field stays the same.
Currently, with the acessors and mutators method, if i put the field on form blank, the entity save this field blank.
How i can solve this? Very thanks CakePHP developers on the world! :-D
you can add code in your entity table to auto hashing password
protected function _setPassword($password)
{
return (new DefaultPasswordHasher)->hash($password);
}
I have a plugin with the name 'Admin' and my methods are like index, add etc. Since I want to apply unique model names and controller names (for ACL implementation), I prefixed all controllers and models with the plugin name (please see cakephp plugin model/controller cache issue with main model/controller). After that, Im facing the following problems. I have a table called users and I am using cakephp2.0.
1) I have to access the url with domain.com/admin/admin_users/index or admin/content/admin_index, instead I want to access by admin/users/index or admin/content/index. How to set this in routes in a general way so that it will be applied to all ?
2) In view, it is showing undefined index User (I have baked the views before). Everything is coming as AdminUser. After setting public $alias = 'User' this issue has been solved. Is this correct ?
3) In controller, I have to change "$this->User->some var/fn " to $this->AdminUser->some var/function is all places. Any way to solve this ?
Anything wrong with plugin name giving as admin (I have not set admin routing) ?
You can safely name your plugin admin, this is by no means a reserved word. However, Cake doesn't take plugin names into account when parsing model / controller names, so AdminUsersController and AdminUser are actually going to be seen as classes belonging to some abstract AdminUser. So to solve all your problems using only Cake magic you'd have to actually name them User and UsersController, and hope these names don't clash. If this is not an option, you can provide some of your own magic to solve these things:
1) How to alias a controller is one of the route configuration examples in the cookbook
2) Yups, if you want to use "User" as a key, that is correct.
3) Not really using only Cake magic, because the $uses variable doesn't support aliasing. You can, however, take advantage of the new lazy-loading of 2.0, by doing something like this in your AdminUsersController:
<?php
public function __get($name) {
// Note, the isset triggers lazy-loading. Check out Controller::__isset()
// if you want to see how that works.
if ($name == 'User' && isset($this->AdminUser)) {
// Assign to admin user here to bypass the next time
return ($this->User = $this->AdminUser);
}
// Delegate to CakePHP's magic getter
return parent::__get($name);
}
I have a multi-domain site. Depending on the domain the site needs to behave accordingly.
I created a helper called CompanyInfo it has methods such as name(), phone(), email(), etc.
Depending on what domain you are on it returns the correct information.
So for example if I need to display the phone number for a user to call I would use $this->CompanyInfo->phone() and it will display the correct phone number for the user depending on the domain.
Ok, this is all good, but not really relevant. The real issue is, I need this information in more than just the view. Helpers are just for views though. If I want to access this information from a controller I need to create a component to do it.
I really don't want to have a Helper and a Component doing the same thing. I would rather have one class handle it rather than copy and paste logic.
So whats the best way to have a class with methods that can be accessed from the controller or view or even model?
Is it just this kind of static information (name, phonenumber, email, etc) what you need to display? Why not just add them to your configuration in core.php?
Something like
# in core.php
Configuration::write('Company.name', 'Acme Corp.');
Configuration::write('Company.email', 'joe#acme.com');
You can then get this info anywhere you need using
Configuration::read('Company.name');
You can define this variable in your app_controller and then use these variable easily in any of your controller as set these variables from there only.
Call this function in your construct class.
i think that will solve your problem.
You can access model classes from any place this way :
$companyInfoModel = ClassRegistry::init('CompanyInfo');
$phone = $companyInfoModel->phone();
a) you can use libs in cake1.3 for that
b) static model methods which you can pass the content to and which will return the expected value
echo Model::phone($data)
I want to access
id of current logged user
name of current logged user
group_id of current logged user
group_name of current logged user
across the view files, to switch menus and tabs on and off according to group_id.
How can I achieve this with minimum sacrifice of performance?
Thanks
If you use the AuthComponent, it'll store the record of the currently logged-in user in the Session under the key Auth. You can access this anywhere through the session component or helper:
$this->Session->read('Auth.User.name')
Even if you're not using the AuthComponent, the Session is the best place to store information about the current user.
Otherwise and in general, the Configure class is usually a good place to store this kind of global information:
Configure::write('User', array('id' => $id, ...));
Configure::read('User.id');
What I always do is create a AppHelper and create a method for this.
Off course this is similar to deceze's answer but it reduces some code you need to write ;)
function user($key) {
$user = $this->Session->read('Auth.User');
if (isset($user[$key])) {
return $user[$key];
}
return false;
}
Then you can call the id of the user by $this->Html->user('id');
Perhaps you could set the variables you want in your AppController (extended by all sub controllers). You should then be able to access them from all views, though be careful to name them uniquely.
CakePHP book - App Controller
i am planing to set a permission on my event index page, which just allow certain user to view which had set when i add the event. After user click into my event, the event controller will 1st check the user id and check the event database which control the user can see which event in his calendar. The permission is added when user create a event and share to other user. Beside, how can i find the current user id to compare with my event database which is the accurate 1?
any suggestion for me to did this function?
i need to know the code and concept how i get the current user id to compare with all the event database, and allow the current user see the certain event.
thanks alot for your information.
The recommended approach for getting logged in user data is via the AuthComponent itself:
// in any controller
$userId = $this->Auth->user('id');
See Accessing the logged in user in the Auth section of the CakePHP Book.
Use sessions to save and read data for a user between pages.
Within Controllers:
// store a user id in the session
$this->Session->write('User.id', $userId);
// read a user id from the session
$userId = $this->Session->read('User.id');
Within Views:
// read a user id from the session
$userId = $session->read('User.id');
You can use any key you want if you prefer something over "User.id". I simply use this since it is what the AuthComponent defaults to if you are using that.
What you're looking for are ACLs (Access Control Lists). There's an AclComponent built into Cake which you should look into. It works together with the AuthComponent, which will hold the user id. It's a little complicated at first, but worth the hassle.
Also, for a simple approach, have a look at the model and controller settings of AuthComponent::authorize. This allows you to define an isAuthorized() method in your controller or model (your choice) which will store logic that determines access (should return true if access allowed and false if denied).
to see sessions, queries, data, and everything else that is passed from page to page in cake use this amazing little helper http://thechaw.com/debug_kit