Commit 24a3605a authored by Viral Solani's avatar Viral Solani

- Make created_by null on user table through migration

- User Registration Class
parent 1b0f7adc
...@@ -12,6 +12,10 @@ DB_DATABASE=homestead ...@@ -12,6 +12,10 @@ DB_DATABASE=homestead
DB_USERNAME=homestead DB_USERNAME=homestead
DB_PASSWORD=secret DB_PASSWORD=secret
# Access
ENABLE_REGISTRATION=true
REQUIRES_APPROVAL=false
BROADCAST_DRIVER=log BROADCAST_DRIVER=log
CACHE_DRIVER=file CACHE_DRIVER=file
SESSION_DRIVER=file SESSION_DRIVER=file
......
...@@ -50,7 +50,7 @@ class RegisterController extends Controller ...@@ -50,7 +50,7 @@ class RegisterController extends Controller
*/ */
public function register(RegisterRequest $request) public function register(RegisterRequest $request)
{ {
if (config('access.users.confirm_email')) { /*if (config('access.users.confirm_email')) {
$user = $this->user->create($request->all()); $user = $this->user->create($request->all());
event(new UserRegistered($user)); event(new UserRegistered($user));
...@@ -59,6 +59,22 @@ class RegisterController extends Controller ...@@ -59,6 +59,22 @@ class RegisterController extends Controller
access()->login($this->user->create($request->all())); access()->login($this->user->create($request->all()));
event(new UserRegistered(access()->user())); event(new UserRegistered(access()->user()));
return redirect($this->redirectPath());
}*/
if (config('access.users.confirm_email') || config('access.users.requires_approval')) {
$user = $this->user->create($request->only('first_name', 'last_name', 'email', 'password','is_term_accept'));
event(new UserRegistered($user));
return redirect($this->redirectPath())->withFlashSuccess(
config('access.users.requires_approval') ?
trans('exceptions.frontend.auth.confirmation.created_pending') :
trans('exceptions.frontend.auth.confirmation.created_confirm')
);
} else {
access()->login($this->user->create($request->only('first_name', 'last_name', 'email', 'password', 'is_term_accept')));
event(new UserRegistered(access()->user()));
return redirect($this->redirectPath()); return redirect($this->redirectPath());
} }
} }
......
...@@ -30,6 +30,14 @@ class UserEventListener ...@@ -30,6 +30,14 @@ class UserEventListener
* @param $event * @param $event
*/ */
public function onRegistered($event) public function onRegistered($event)
{
\Log::info('User Registered: '.$event->user->full_name);
}
/**
* @param $event
*/
/*public function onRegistered($event)
{ {
\Log::info('User Registered: '.$event->user->first_name); \Log::info('User Registered: '.$event->user->first_name);
...@@ -39,7 +47,7 @@ class UserEventListener ...@@ -39,7 +47,7 @@ class UserEventListener
'email_template_type' => 1, 'email_template_type' => 1,
]; ];
createNotification('', 1, 2, $options); createNotification('', 1, 2, $options);
} }*/
/** /**
* @param $event * @param $event
......
...@@ -79,6 +79,8 @@ class UserRepository extends BaseRepository ...@@ -79,6 +79,8 @@ class UserRepository extends BaseRepository
} }
/** /**
* Create User
*
* @param array $data * @param array $data
* @param bool $provider * @param bool $provider
* *
...@@ -94,9 +96,26 @@ class UserRepository extends BaseRepository ...@@ -94,9 +96,26 @@ class UserRepository extends BaseRepository
$user->confirmation_code = md5(uniqid(mt_rand(), true)); $user->confirmation_code = md5(uniqid(mt_rand(), true));
$user->status = 1; $user->status = 1;
$user->password = $provider ? null : bcrypt($data['password']); $user->password = $provider ? null : bcrypt($data['password']);
$user->confirmed = $provider ? 1 : (config('access.users.confirm_email') ? 0 : 1);
$user->is_term_accept = $data['is_term_accept']; $user->is_term_accept = $data['is_term_accept'];
// If users require approval, confirmed is false regardless of account type
if (config('access.users.requires_approval')) {
$user->confirmed = 0; // No confirm e-mail sent, that defeats the purpose of manual approval
} elseif (config('access.users.confirm_email')) { // If user must confirm email
// If user is from social, already confirmed
if ($provider) {
$user->confirmed = 1; // E-mails are validated through the social platform
} else {
// Otherwise needs confirmation
$user->confirmed = 0;
$confirm = true;
}
} else {
// Otherwise both are off and confirmed is default
$user->confirmed = 1;
}
DB::transaction(function () use ($user) { DB::transaction(function () use ($user) {
if ($user->save()) { if ($user->save()) {
/* /*
......
...@@ -88,7 +88,7 @@ return [ ...@@ -88,7 +88,7 @@ return [
/* /*
* Whether or not public registration is on * Whether or not public registration is on
*/ */
'registration' => env('ENABLE_REGISTRATION', 'true'), 'registration' => env('ENABLE_REGISTRATION', true),
/* /*
* The role the user is assigned to when they sign up from the frontend, not namespaced * The role the user is assigned to when they sign up from the frontend, not namespaced
...@@ -105,6 +105,12 @@ return [ ...@@ -105,6 +105,12 @@ return [
* Whether or not the users email can be changed on the edit profile screen * Whether or not the users email can be changed on the edit profile screen
*/ */
'change_email' => false, 'change_email' => false,
/*
* Whether or not new users need to be approved by an administrator before logging in
* If this is set to true, then confirm_email is not in effect
*/
'requires_approval' => env('REQUIRES_APPROVAL', false),
], ],
/* /*
......
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNullConstraintOnCreatedByOnUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('created_by')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
...@@ -52,8 +52,8 @@ class UserTableSeeder extends Seeder ...@@ -52,8 +52,8 @@ class UserTableSeeder extends Seeder
'deleted_at' => null, 'deleted_at' => null,
], ],
[ [
'first_name' => 'John', 'first_name' => 'User',
'last_name' => 'Doe', 'last_name' => 'Test',
'email' => 'user@user.com', 'email' => 'user@user.com',
'password' => bcrypt('1234'), 'password' => bcrypt('1234'),
'confirmation_code' => md5(uniqid(mt_rand(), true)), 'confirmation_code' => md5(uniqid(mt_rand(), true)),
......
<?php
namespace Tests\Feature;
use Faker\Generator;
use Tests\BrowserKitTestCase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use App\Events\Frontend\Auth\UserLoggedIn;
use App\Events\Frontend\Auth\UserRegistered;
class RegistrationTest extends BrowserKitTestCase
{
/** @test */
public function registration_page_loads_properly()
{
$this->visit('/register')
->see('first_name')
->see('last_name')
->see('email')
->see('password')
->see('is_term_accept')
->see('Register');
}
/** @test */
public function registration_fails_when_a_required_field_is_not_filled_in()
{
$this->visit('/register')
->type('', 'first_name')
->type('', 'last_name')
->type('', 'email')
->type('', 'password')
->press('Register')
->seePageIs('/register')
->see('The first name field is required.')
->see('The last name field is required.')
->see('The email field is required.')
->see('The password field is required.')
->see('The is term accept field is required.');
}
/**
* Test the registration form
* Test it works with confirming email on or off, and that the confirm email notification is sent
* Note: Captcha is disabled by default in phpunit.xml.
*/
/** @test */
public function test_registration_form()
{
// Make sure our events are fired
Event::fake();
config(['access.users.confirm_email' => false]);
config(['access.users.requires_approval' => false]);
$this->visit('/register')
->type('John', 'first_name')
->type('Doe', 'last_name')
->type('john.doe@example.com', 'email')
->type('Viral@1234', 'password')
->type('Viral@1234', 'password_confirmation')
->check('is_term_accept')
->press('Register')
->seePageIs('/')
->seeInDatabase(config('access.users_table'),
[
'email' => 'john.doe@example.com',
'first_name' => 'John',
'last_name' => 'Doe',
'confirmed' => 1,
]);
Event::assertDispatched(UserRegistered::class);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment