I am editing a website which someone has made in CakePHP, but I don't have any previous experience in Cake. I'm reading the manual but finding it quite hard to understand so thought I would post a question on here to see if I can get any quick answers.
I think that this code is being used to display a login box, and you can only log in with username test and password 123123
var $components = array("Auth", "Acl");
function beforeFilter(){
$this->Security->loginOptions = array(
'type' => 'basic',
'realm' => 'Authenticate Emergency Response Center'
);
$this->Security->loginCredentials = array(
'test' => '123123'
);
$this->Security->requireLogin();
$this->_bindToSite();
parent::beforeFilter();
}
I want the log in box to appear still, but I want to fill the loginCredentials array automatically with information from the database. I have a table called 'operators' which contains the fields 'user_id' and 'password'.
Could someone tell me how I would alter the code above to allow any of the usernames/passwords stored in the operators table to log in?
Thanks for any help
Build an array from the database and set $this->Security->loginCredentials equal to your array.
You can leave most of the hard work up to CakePHP's Auth component, but if you want to use a model other than the default 'User', you'll just need to set this in beforeFilter(). The best place to do this is probably app_controller.php in your app\ directory.
function beforeFilter(){
$this->Auth->userModel = 'Operator';
$this->Auth->fields = array(
'username' => 'user_id',
'password' => 'password'
);
);
Related
I need to get the value from link and then add them to DB.
I am working with another developer who created an api as a bridge because he does not know Laravel. Here is the api http://laravel.io/bin/614Xv I am currently passing string valued data like so http://laravel.io/bin/wYry0 and passing the parameters $user, $name, $password through the route.
Here is my route
Route::get('/profile/activated/{user?}/{pass?}/{email?}', array(
'as' => 'invited-user-account-created-get',
'uses' => 'ProfileController#getCreateInvitedUser'
));
Here is my function:
public function getCreateInvitedUser($user=null, $password=null, $email=null) {
$user = Input::get('username', 'abcd');
$pass = Input::get('password', '1234');
$email = Input::get('email', 'asas#gam.com');
//insert $user array in database users table
$user = User::create(array(
'username' => $user,
'password' => $pass,
'email' => $email
));
}
The other developer is wanting me to set the input to the variables I get from the url link/profile/activated/user/pass/email
Maybe I am tired but I am not understanding what he wants me to do and how to grab the variables from the url. The whole reason in doing this is to instantly store the invited guest info in the database when they click the activation link so they do not have to sign up for an account later.
Your route has a problem
Use this instead
Route::get('/profile/activated/{user}/{pass}/{email}', array(
'as' => 'invited-user-account-created-get',
'uses' => 'ProfileController#getCreateInvitedUser'
));
But his idea is totally messed up! Passing password values through a URL, Who does that even?
You sound enlightened, please advise him to try a different approach in whatever thing he's trying to achieve.
I know this is a stupid question but after messing around, I am confused what is the right way of doing.
Below are my code :
View registration_form.php :
$input_data = array(
'name' => 'user_name',
'id' => 'user_name',
'value' => set_value('user_name'),
'maxlength' => MAX_CHARS_4_USERNAME
);
Controller register.php:
$this->load->model('user_model');
$this->user_model->create_user( 'customer', array() );
Form Validation Config :
$config['customer_creation_rules'] = array(
array(
'field' => 'user_name',
'label' => 'USERNAME',
'rules' => 'trim|required|alpha|strtolower'
)
);
Model user_model.php :
public function create_user( $role, $insert_array = array() )
{
// The form validation class doesn't allow for multiple config files, so we do it the old fashion way
$this->config->load( 'form_validation/administration/create_user/create_' . $role, TRUE );
$this->validation_rules = config_item( $role . '_creation_rules' );
$form_username = $this->config->item($role . '_creation_rules', 'field');
echo $form_username;
}
What I want to do right now is to check the username that the user input and auto add a number to the input username before inserting into database.
Initially I thought of getting the inputted username from the Form Validation, after messing around, I can't get the value no matter what I have tried.
Am I doing it wrongly? Do I just get from $_POST instead?
Hope you guys can help me out on this. Thanks in advance!
This shows the full list (waaaay at the bottom): http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
I don't see that one there, but it says you can use any PHP function that requires only one parameter so that should work, but you'll have to look deeper into the docs to figure out why it isn't.
I don't think you can set rules for a form, you have to set them per variable, as the docs show:
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
I'm trying to add a User Note while programmatically adding a user. I've been working quite successfully from Joomla 3.0 Register user with php script, but am unable to figure out how J32 adds a note - in model notes.php, the save function is commented out, so it's a deadend for me while I figure out all this marvelous new version of the best CMS out there.
Is there some method to do this - an equivalent to registering like $model->addnote,
i couldn't find a method but here is the script i have used within my own plugin. All seems to work correctly
protected function _addNote($vars) {
$now = new JDate();
$now = $now->toSql();
// Create and save the user note
$userNote = (object) array(
'user_id' => $vars['id'],
'catid' => 0,
'subject' => "User Information Update By Proworx",
'body' => "<p>User ".$vars['name']." has been updated to:</p><p>Name: ".$vars['name']."</p><p>Email: ".$vars['email']."</p><p>Password: Not Telling... check proworx</p>",
'state' => 1,
'created_user_id' => 332, //DONT FORGET TO CHANGE THIS VALUE TO YOUR SITES ADMIN
'created_time' => $now
);
$db = JFactory::getDbo();
$db->insertObject('#__user_notes', $userNote, 'id');
}
I would like to connect to a second database with Yii at runtime. The database name would come from a database table after the user to login.
I saw in a tutorial I should do this:
$db2 = Yii::createComponent(array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => $emp['database']
));
Yii::app()->setComponent('db2',$db2);
But in my controler when I access Yii::app()->db2 get the error:
Property "CWebApplication.db2" is not defined
What am I doing wrong?
The following works for me:
Yii::app()->mongodb->setActive(false);
Yii::app()->mongodb->setServer('mongodb://localhost:27017');
Yii::app()->mongodb->setDb('db1');
Yii::app()->mongodb->setActive(true);
UPDATED: Try, instead instance, pass configurations:
Yii::app()->setComponent( 'db2', array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => $emp['database']
)
);
Or, you may create special index on params in configurations, such as:
...
'params' => array(
'db2' => null,
),
And the use Yii::app()->params['db2'] = $db2
From this comment:
My problem is not with the creation of the component. Soon after
creating if I access Yii::app()->db2 its works, but when I try to
access via another model or controller I get the error
I think you are setting this component only once somewhere, and then making subsequent requests to different controllers.
You need to put the code, somewhere it is being called EVERYTIME, on every Request. thats how PHP works, there is no "global application state"
by default Yii comes with protected/components/controller.php has base controller for the rest of the app.
my suggestion would be to put your code on the init() method of that controller, so that it always gets called.
You mentioned the database name comes from a table once the user logs in, so you need to save that value in the session, in other to be able to access it in the other requests:
<?php
// After login in
Yii::app()->user->setState('db_name', $db_name);
// in protected/components/controller.php
public function init()
{
if (!Yii::app()->user->isGuest) {
$db2 = Yii::createComponent(array(
'class' => 'EMongoClient',
'server' => 'mongodb://127.0.0.1:27017',
'db' => Yii::app()->user->getState('db_name')
));
Yii::app()->setComponent('db2',$db2);
}
}
Hope it helps, I am assuming many things here :)
Im back to professional programming after 5 year late, so i have learn from beginning ;)
So i chose Kohana framework for my first framework i try to bulid first application now and i have a small problem, lets begin.
I use Kohana ORM and Auth module, as you know default Auth module user table have default fields (username, password, lastlogin) i try to extend User by:
Creating new table (user_additionals)
Creating new model (User_Additional)
Model look like this: http://pastie.org/private/412jflfoifyqs46uaxmga - Nothing special. Everything will be okay, i like easy reference like this: $user->additional->firstname etc.
At the moment i have bulid admin panel (admin can edit every user) and... every field. I have 10 fields like firstname, lastname, birthdate presented as form (filled form - placeholder loaded by template assign) and here is my small problem:
I want to give admin possibility to edit one from much fields, if admin need to edit user signature or something else - he edit one field from a lot fields available and click "Submit" it's easy - one form have been updated.
But, if i try use something like this:
$edit = ORM::Factory('User_Additional')->values($_POST); I get validation Exception (which be catched but, here are validation error - model required all fields to be !empty... (By validation rules)
I use temporary solution, but im a perfectionist and i want to create good code from begining, so here you can find my code: http://pastie.org/private/axtwxbt66gtvcwiv97hvlq
My solution start at line 29 (link above).
So my question is:
*How to make exceptions from Validation in cases like this?? *
*How make exceptions from validation for example for action /admin/edituser/ is it possible? *
*How do i can do my model code better? Thanks for any suggestions which can help me *
Thanks!
Validation will only run on "non-empty" fields, unless you specify the "not_empty" rule.
So basically, you could do the following: (when you need to enforce the "non emptiness" of a field)
class Model_User_Additional extends ORM
{
protected $foreign_key_suffix = 'user_id';
protected $_primary_key = 'user_id';
public function enforce_rules(array $fields)
{
$validation = Validation::factory($this->_object);
foreach ($fields as $field)
{
$validation->rule($field, 'not_empty');
}
return $validation;
}
public function rules()
{
/* Removed all not_empty */
return array(
/* 'user_id' => array(
array('not_empty'),
), */
'firstname' => array(
array('text'),
),
'lastname' => array(
array('text'),
),
'birthdate' => array(
array('date'),
),
'postprice' => array(
array('decimal'),
),
'articleprice' => array(
array('decimal'),
),
'phonenumber' => array(
array('phone'),
),
/* 'active' => array(
array('not_empty'),
), */
/* 'lastupdate' => array(
array('not_empty'),
), */
'info' => array(
array('text'),
),
);
}
public function edit_user($values, $expected, $extra_validation = NULL)
{
return $this->values($values, $expected)->update($extra_validation);
}
}
the usage would be:
$user = ORM::factory('User_Additional');
$user->values(array('field' => 'value'))->create($user->enforce_rules(array('user_id')));
/* and the same for updating */
$user->set('info', $_POST['info'])->update($user->enforce_rules(array('info')));