-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from MSR2012/main
Registration feature done
- Loading branch information
Showing
12 changed files
with
376 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Company; | ||
use App\Models\Person; | ||
use App\Models\User; | ||
use App\Providers\RouteServiceProvider; | ||
use App\Traits\ActivationTrait; | ||
use Illuminate\Foundation\Auth\RegistersUsers; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Validator; | ||
use LaravelEnso\Roles\Models\Role; | ||
use LaravelEnso\UserGroups\Models\UserGroup; | ||
|
||
class RegisterController extends Controller | ||
{ | ||
use RegistersUsers; | ||
use ActivationTrait; | ||
|
||
protected $redirectTo = RouteServiceProvider::HOME; | ||
|
||
public function __construct() | ||
{ | ||
$this->middleware('guest'); | ||
} | ||
|
||
protected function validator(array $data) | ||
{ | ||
return Validator::make($data, [ | ||
'first_name' => ['required', 'string', 'max:255'], | ||
'last_name' => ['required', 'string', 'max:255'], | ||
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], | ||
'password' => ['required', 'string', 'min:5', 'confirmed'], | ||
]); | ||
} | ||
|
||
public function create(Request $request) | ||
{ | ||
$validator = $this->validator($request->all()); | ||
if ($validator->fails()) { | ||
return $validator->errors(); | ||
} | ||
|
||
$person = new Person(); | ||
$name = $request['first_name'] . ' ' . $request['last_name']; | ||
$person->name = $name; | ||
$person->email = $request['email']; | ||
$person->save(); | ||
|
||
$user_group = UserGroup::where('name', 'Administrators')->first(); | ||
if ($user_group === null) { | ||
$user_group = UserGroup::create(['name' => 'Administrators', 'description' => 'Administrator users group']); | ||
} | ||
|
||
$role = Role::where('name', 'free')->first(); | ||
if ($role === null) { | ||
$role = Role::create(['menu_id' => 1, 'name' => 'free', 'display_name' => 'Supervisor', 'description' => 'Supervisor role.']); | ||
} | ||
|
||
$user = User::create([ | ||
'email' => $request['email'], | ||
'password' => bcrypt($request['password']), | ||
'person_id' => $person->id, | ||
'group_id' => $user_group->id, | ||
'role_id' => $role->id, | ||
'is_active' => 1, | ||
]); | ||
|
||
$company = Company::create([ | ||
'name' => $request['email'], | ||
'email' => $request['email'], | ||
'is_tenant' => 1, | ||
'status' => 1, | ||
]); | ||
|
||
$person->companies()->attach($company->id, ['person_id' => $person->id, 'is_main' => 1, 'is_mandatary' => 1, 'company_id' => $company->id]); | ||
|
||
if ($request->selected_plan === '' || $request->selected_plan === $user->role_id) { | ||
$user->plan_id = ''; | ||
} else { | ||
$user->plan_id = $request->selected_plan; | ||
} | ||
|
||
return $user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\Activation; | ||
use App\Models\User; | ||
use App\Providers\RouteServiceProvider; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Validator; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class VerificationController extends Controller | ||
{ | ||
protected $redirectTo = RouteServiceProvider::HOME; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* verify user token. | ||
*/ | ||
public function verify_user(Request $request) | ||
{ | ||
$token = null; | ||
$activation = null; | ||
$user_id = null; | ||
$user = null; | ||
$data = $request->all(); | ||
$this->validator($data)->validate(); | ||
$token = $request->get('token'); | ||
$activation = Activation::where('token', $token)->first(); | ||
if ($activation === null) { | ||
return response()->json( | ||
[ | ||
'error' => [ | ||
'code' => 300, | ||
'message' => 'Send activation code again.', | ||
], | ||
], | ||
Response::HTTP_UNPROCESSABLE_ENTITY | ||
); | ||
} | ||
$user_id = $activation->user_id; | ||
$user = User::find($user_id); | ||
if ($user === null) { | ||
return response()->json( | ||
[ | ||
'error' => [ | ||
'code' => 301, | ||
'message' => 'There is not such user.', | ||
], | ||
], | ||
Response::HTTP_UNPROCESSABLE_ENTITY | ||
); | ||
} | ||
$user->is_active = 1; | ||
$user->email_verified_at = date('Y-m-d H:i:s'); | ||
$user->save(); | ||
Activation::where('user_id', $user_id)->delete(); | ||
|
||
return response()->json([ | ||
'csrfToken' => csrf_token(), | ||
]); | ||
} | ||
|
||
protected function validator(array $data) | ||
{ | ||
return Validator::make($data, [ | ||
'token' => ['required', 'string', 'max:255'], | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $user_id | ||
* @property string $token | ||
* @property string $ip_address | ||
* @property string $created_at | ||
* @property string $updated_at | ||
*/ | ||
class Activation extends Model | ||
{ | ||
/** | ||
* The "type" of the auto-incrementing ID. | ||
* | ||
* @var string | ||
*/ | ||
protected $keyType = 'integer'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $fillable = ['user_id', 'token', 'ip_address', 'created_at', 'updated_at']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Traits\CreatedBy; | ||
use LaravelEnso\Companies\Models\Company as CoreCompany; | ||
|
||
class Company extends CoreCompany | ||
{ | ||
use CreatedBy; | ||
|
||
protected $fillable = [ | ||
'privacy', | ||
'name', | ||
'email', | ||
'is_tenant', | ||
'status', | ||
]; | ||
} |
Oops, something went wrong.