How to create a drupal user with out email address? - php

I want to create a Drupal user account without an email address. How can I create that? If I write a custom query which tables do I have to insert values in? Or does Drupal have a built-in function for that?
I have only user's first name, last name, password details. I want to create a user with only this information.

This will work, even email is required:
$newUser = array(
'name' => 'your username',
'pass' => 'any password', // note: do not md5 the password
'status' => 1,
);
user_save(null, $newUser);

Related

CodeIgniter - Checking user entered email once completed typing, to confirm if taken or not

Trying to perfect an email form input, where the user can enter their email that is to be associated to their account. However ideally, the system would reject/accept the entered email once they have finished typing (not on form submission). I am using CodeIgniter, and still learning alot unfortunately.
My VIEW/form has the following input:-
<?=form_input(array(
'name' => 'pi_email',
'id' => 'pi_email',
'value' => isset($email)?$email:'',
'maxlength' => '50',
'required' => 'yes',
'size' => '50',
'placeholder' => 'johndoe#example.com',
'class' => 'lg-textbox'
));?>
On the controller/profile, I have the below as a test to try and catch these inputs and echo if taken or not, right now option 1 is always displayed as "email already taken", when they are not actually. I have seen the below form_validation online, however I do not generally understand the context in which it is to be used successfully
$this->form_validation->set_rules($new_email, 'Email', 'required|valid_email|is_unique[tbl_users.email]');
if($this->form_validation->run() == false){
echo "email already taken";
} else {
echo "Valid email!";
}
How could i implement real time check that will display on screen? I know how to use db model to run a query and return results, if = 0 valid, if > 0 then email taken? can this be implemented on the view or has to be done in the controller and then return user to the profile page and display an error message?
Any assistance, general pointing in the right direction would be great

How Can I manual pass information to the register controller in Laravel

To create a user without the end-user having to type in the details.
Something akin to
User::classcreate([
'email' => $email,
'password' => $rand_pass,
.....
]);
Thanks for the ideas and feedback in advance :)
The use case is.
The end-user invites another user to use the service by typing in their email and it creates a user with a random password before sending a email to the new user with their created details.
You're almost there with your code. It should look like this:
User::create([
'email' => $email,
'password' => bcrypt($rand_pass),
]);
But if you want to hash the password, you should also send the password to that user through email (which is not very secure). When the users logs in for the first time, you should at least require him to change the password.
You can simply use create() method of your User model:
$userData = [
'email' => 'some#email.com',
'password' => 'somepassword'
];
$newUser = User::create($userData);
You'll also need your password hashed in order for it to work with Laravel's authorization. Add the following to your user model - it will hash password before it's saved to the database:
public function setPasswordAttribute($password)
{
$this->attributes['password'] = Hash::make($password);
}

Laravel random to database

What i'm try to do: Then user registered on my website he's got a email with confirmation random code.
My controller looks like that:
$activation_code = str_random(40);
/*
* Register user.
*/
$user = Sentry::register([
'username' => Input::get('username'),
'email' => Input::get('email'),
'password' => Input::get('password'),
'language_id' => $language->id,
'activation_code' => $activation_code
]);
$user->slug = Str::slug($user->username);
$user->save();
But then i check databse, the activation_code cell is empty. What i'm doing wrong? Thanks for answers!
I assume you meant the activation column not table, check your fillable array and make sure its in there.
EDIT
Scratch that, it looks like like your auto activating the user, no activation code necessary in that case.
FIXED! Works fine with:
$activationCode = $user->getActivationCode();

Changing yii default username field to email field for login

To login in yii, i want to do it on a database table (mysql) instead of the default "admin" and "demo" default values provided in the UserIdentity class. The thing is, my user table in the database that i created does not use username and password fields for authentication, but rather email and password fields. So when i change all the "username" to "email" variables in UserIdentity.php, LoginForm.php, SiteController.php, login.php, i get an error:
Property "UserIdentity.email" is not defined.
and the error points to the change i made here by replacing username with email as shown below:
public function authenticate()
{
$user = User::model()->findByAttributes(array(
'email' => $this->email
));
Help on this would be much appreciated.
You may change email on login in UserIdentity.
$user = User::model()->findByAttributes(array(
'email' => $this->username
));
Or add new field 'email' to UserIdentity and use it.

What happens if Drupal's user_save() fails?

I'm on Drupal 5.x and I'm trying to add new users to the site using a script that calls drupal_bootstrap(). After generating the username, email, password, and role array, I create the new user like so:
$newuser = array( 'name' => $username, 'mail' => $email, 'status' => 1, 'pass' => $password, 'roles' => $roles);
$user = user_save('', $newuser);
I know that with this code I can test the $user object returned by user_save(), but how do I test if the user was created and inserted correctly? Do I have to query the database to test if the user was created successfully?
In drupal 5 it's not easy to see it it fails like in drupal 6. But since it returns a fresh user object from the db, you can inspect it to see if the data was saved properly. So if you try to insert a user and it failed, it will return FALSE.

Categories