I use Codeigniter framework + Tank_Auth registiration library. They work great but I just need to change register form a bit.
I want to generate username from email address instead of getting it from registeration form. I added a simple function to libraries/Tank_auth.php file. Here is my username generator function:
function _create_username_from_email($email)
{
$user = strstr($email, '#', true); // As of PHP 5.3.0
return $user;
}
Please let me know where I need to run my function and write it to database.
I will also need to remove username field from register form. So I need how I can pass form validation for username field in my controller.
I will be really happy if any of you can help me about these problem.
Thanks
At line 121 in application/models/tank_auth/users you have:
/**
* Create new user record
*
* #param array
* #param bool
* #return array
*/
function create_user($data, $activated = TRUE)
{
$data['created'] = date('Y-m-d H:i:s');
$data['activated'] = $activated ? 1 : 0;
if ($this->db->insert($this->table_name, $data)) {
$user_id = $this->db->insert_id();
if ($activated) $this->create_profile($user_id);
return array('user_id' => $user_id);
}
return NULL;
}
Just before the if ($this->db->insert($this->table_name, $data)) { insert:
$data['username'] = strstr($data['email'], '#', true);
P.S.
I hope you understand it, but just in case - if you have two or more users with same email's username part like john#hotmail.com and john#gmail.com they both will have the same username, which may lead to unexpected behavior.
Related
I want to check a fetched user exists in DB. Suppose I assigned the authenticated user to a variable and the same user is deleted with another variable. But the first variable holds the value of the deleted user. So I want to check whether user data exists in the DB. Something like this.
$user = Auth::user();
$user1 = Auth::user();
$user1->delete();
if($user->existsInDB()){
//
}
If you have a look at the official documentation you will see that there's a fresh method:
The fresh method will re-retrieve the model from the database. The existing model instance will not be affected:
If we have a look at the source code, the fresh method returns null if the model doesn't exists (for example if you create a new Model) or if it can't be found:
/**
* Reload a fresh model instance from the database.
*
* #param array|string $with
* #return static|null
*/
public function fresh($with = [])
{
if (! $this->exists) {
return;
}
return static::newQueryWithoutScopes()
->with(is_string($with) ? func_get_args() : $with)
->where($this->getKeyName(), $this->getKey())
->first();
}
Now in your code you can just add a null check to achive your goal... something like:
$user = Auth::user();
$user1 = Auth::user();
$user1->delete();
if($user->fresh()){
// !== null => it exists
} else {
// === null => it doesn't exists
}
use can do that with method 'find()' like this :
$user=Users:find(Auth::id())
if($user)
{
//user is exist
}
$email='abc#abc.com';
$pass= 123;
$this->db->select('*')->from('user')->where('email',$email)->where('password',$pass)-> get()->result();
this gives result :-
'select * from user where email IS NULL and password IS NULL';
But it works with $this->db->query();
plz help me to find why its not taking like :-
'select * from user where email="abc#abc.com" and password="123" ';
Use this method. Hope it will help you
$email = "abc#example.com";
$password = "abc123";
public function getdata()
{
// first load your model if not added in your config/autoload file
return $this->db->get_where('table_name', array('email' => $email, 'password' => $password))->row_array();
}
Edited
#
Laravel Version: 5.2.45
PHP Version: 5.6
Database Driver & Version:
Description:
Hi,
I finished update from laravel 5.1 to 5.2 and resolved some problems...
Now i try make login, but the Auth verify the password of the 'password' field but in my db the password has another name that is 'senha'.
illuminate/Auth/EloquentUserProvider.php
/**
* Retrieve a user by the given credentials.
*
* #param array $credentials
* #return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials)) {
return;
}
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();
foreach ($credentials as $key => $value) {
**if (! Str::contains($key, 'password'))** {
$query->where($key, $value);
}
}
return $query->first();
}
how you see, the function tried find the string 'password' in the credentials when it should be 'senha'
The same problem with the validateCredentials function in file illuminate/Auth/EloquentUserProvider.php
/**
* Validate a user against the given credentials.
*
* #param \Illuminate\Contracts\Auth\Authenticatable $user
* #param array $credentials
* #return bool
*/
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['password'];
return $this->hasher->check($plain, $user->getAuthPassword());
}
What is the best way to fix this?
The best way is ...
\Auth::attempt( [ 'email' => ' campos#b.com.br ' , 'password' => ' 1234567890 '] );
And in their model authentication :
/ **
* Get the password for the user .
*
* #return String
* /
getAuthPassword public function ( )
{
return $this->senha;
}
As Dayglor was pointing out, the EloquentUserProvider doesn't query for the password field. It expects the credentials to always have a 'password' field, any other fields are what it uses to query the database. Only after it finds a user will it then check the password by doing a hash_check. It takes the credentials['password] to hash check against $user->getAuthPassword(). So just set on your User model the getAuthPassword method to return the correct field you want to be used as the password field to check against.
I have a good amount of experience with MVC but can't seem to figure what is going wrong here.
I'm trying to update the user's model but for some reason what should be an update is attempting to execute as an insert.
Edit: I'm using this starter site for Laravel which uses Ardent
$user = User::find(Auth::user()->id);
$user->recipient_id = "xxxxxxxxx";
if ($user->save()) {
return true;
} else {
print_r($user->errors()->all()); die();
}
The above outputs
Array (
[0] => The username has already been taken.
[1] => The email has already been taken.
[2] => The password confirmation does not match.
)
Any help would be really appreciated! I have a feeling its something trivial...
Actually while validating it uses rules of the User Model class.
This happens because use of these lines in models.
public $autoHydrateEntityFromInput = true; // hydrates on new entries' validation
public $forceEntityHydrationFromInput = true; // hydrates whenever validation is called
What you have to do is to change the rules dynamically.
$user = User::find(Auth::user()->id);
$user->recipient_id = "xxxxxxxxx";
$user::$rules['username'] = 'required|regex:/^[a-zA-Z\- ]+$/|unique:users,username,'.$user_id;
$user::$rules['email'] = 'required|email|unique:users,email,'.$user_id;
$user::$rules['password'] = 'required';
//this will ignore current username and email
if ($user->save()) {
return true;
} else {
print_r($user->errors()->all()); die();
}
I am new to Magento and PHP. I use the following line to get the email, which works fine except in the case a customer just registered. Any suggestions? Thanks.
$userEmail = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
I assume that this code runs before the customer object data was saved propperly.
There is a line of code callled: in the OnePageCheckout and it does the following:
/**
* Involve new customer to system
*
* #return Mage_Checkout_Model_Type_Onepage
*/
protected function _involveNewCustomer()
{
$customer = $this->getQuote()->getCustomer();
if ($customer->isConfirmationRequired()) {
$customer->sendNewAccountEmail('confirmation', '', $this->getQuote()->getStoreId());
$url = Mage::helper('customer')->getEmailConfirmationUrl($customer->getEmail());
$this->getCustomerSession()->addSuccess(
Mage::helper('customer')->__('Account confirmation is required. Please, check your e-mail for confirmation link. To resend confirmation email please click here.', $url)
);
} else {
$customer->sendNewAccountEmail('registered', '', $this->getQuote()->getStoreId());
$this->getCustomerSession()->loginById($customer->getId());
}
return $this;
}
If the customer is just registering, not using the checkout process, then there is a different function using the request parameters like Anton said:
/app/code/core/Mage/Customer/Block/Form/Register.php
/**
* Restore entity data from session
* Entity and form code must be defined for the form
*
* #param Mage_Customer_Model_Form $form
* #return Mage_Customer_Block_Form_Register
*/
public function restoreSessionData(Mage_Customer_Model_Form $form, $scope = null)
{
if ($this->getFormData()->getCustomerData()) {
$request = $form->prepareRequest($this->getFormData()->getData());
$data = $form->extractData($request, $scope, false);
$form->restoreData($data);
}
return $this;
}
you can get it from request parameters directly?