Kolesar
Kolesar

Reputation: 1283

Cartalyst Sentry and registration user

It is possible to create user from Admin panel, by administrator without password? I imagine follow procedure:

Upvotes: 0

Views: 554

Answers (2)

alexrussell
alexrussell

Reputation: 14212

I have done this before by hacking the 'forgotten password' functionality of Laravel (rather that reinventing the wheel). I can't say how well this fits into Sentry but it was pretty trivial to do it in plain old Laravel:

  1. Create user with blank password
  2. Add an entry into the password reminders table (manually, don't use Auth::remind or whatever it is as it'll send an email, but do use the code from the class to generate the token)
  3. Send welcome email to user with link to /user/confirm (or whatever, the point is that it doesn't have to be /user/forgotten-password) and hook that route up in the normal way for forgotten password with an added check for $user->password == '' if you wanna make sure only unconfirmed people can go to that page (not that it really matters).

You may also wish to extend the timeout on the forgotten passwords or, as I did (proper hacky I know), when the user's in the /user/confirm version of the forgotten password functionality, just refresh the timeout in the table before passing through to Laravel's auth system for checking.

Our code is something like this:

On register:

// however you register the user:
$user = new User;
$user->email = Input::get('email');
$user->password = '';
$user->save();

// create a reminder entry for the user
$reminderRepo = App::make('auth.reminder.repository');
$reminderRepo->create($user);

Mail::send(
    'emails.registered',
    [
        'token' => $reminder->token,
    ],
    function ($message) use ($user) {
        $message->to($user->email)->setSubject(Lang::get('account.email.registered.subject', ['name' => $user->name]));
    }
);

Now the confirm link:

class AccountController extends Controller
{
    public function confirm($token)
    {
        $reminder = DB::table('password_reminders')->whereToken($token)->first();

        if (! $reminder) {
            App::abort(404);
        }

        // reset reminder date to now to keep it fresh
        DB::table('password_reminders')->whereToken($token)->update(['created_at' => Carbon\Carbon::now()]);

        // send token to view but also email so they don't have to type it in (with password reminders it's is a good thing to make users type it, but with confirm account it feels weird)
        return View::make('account.confirm-account')->withToken($token)->withEmail($reminder->email);
    }

    public function postConfirm($token)
    {
        $credentials = Input::only('email', 'password', 'password_confirmation', 'token');

        $response = Password::reset($credentials, function ($user, $password) {
            $user->password = $password;
            $user->save();
        });

        switch ($response) {
            case Password::INVALID_PASSWORD:
            case Password::INVALID_TOKEN:
            case Password::INVALID_USER:
                return Redirect::back()->withInput()->with('message-error', Lang::get($response));

            case Password::PASSWORD_RESET:
                Auth::login(User::whereEmail(Input::get('email'))->first());
                return Redirect::route('account.home')->with('message-info', Lang::get('messages.confirm_account.succeeded'));
    }
}

Upvotes: 0

thaifood
thaifood

Reputation: 26

I don't think so. That's why when I create my users I generate a random password.

$user->password = str_shuffle("Random_Password"); // generate random initial password

Upvotes: 1

Related Questions