Skip to content

Commit

Permalink
Setup password optionality and make some update in migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
mawuva committed Sep 13, 2021
1 parent 048df56 commit 5404b61
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 22 deletions.
11 changes: 7 additions & 4 deletions config/usercare.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
],

'default' => [
'file_source_url' => url('/'),
'avatar' => asset('/'), // asset('default-avatar.png')
'bg_picture' => asset('/'), // asset('default-bg-picture.png')
'file_source_url' => env('APP_URL'),
'avatar' => env('APP_URL'), // asset('default-avatar.png')
'bg_picture' => env('APP_URL'), // asset('default-bg-picture.png')
],
],

Expand All @@ -77,9 +77,12 @@

'enable' => [
'proper_names' => true,
'email_optionality' => false,
'phone_number' => true,
'gender' => true,
'optional' => [
'email' => false,
'password' => true,
],
],

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ class AddProperNamesColumnsToUsersTable extends Migration
public function up()
{
$table = config('usercare.user.table.name');
$proper_names_enabled = config('usercare.proper_names.enable');

if ($proper_names_enabled) {
if (proper_names_is_enabled_and_does_not_exist()) {
Schema::table($table, function (Blueprint $table) {
if (!schema_has_proper_names_columns()) {
$table->string('last_name') ->nullable() ->after('name');
$table->string('first_name') ->nullable() ->after('last_name');
}
$table->string('last_name') ->nullable() ->after('name');
$table->string('first_name') ->nullable() ->after('last_name');
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SetEmailToOptional extends Migration
public function up()
{
$table =config('usercare.user.table.name');
$email_is_optional = config('usercare.user.email_is_optional');
$email_is_optional = config('usercare.enable.optional.email');

if ($email_is_optional) {
Schema::table($table, function (Blueprint $table) {
Expand Down
35 changes: 35 additions & 0 deletions database/migrations/2021_09_13_012837_set_password_to_optional.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class SetPasswordToOptional extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$table =config('usercare.user.table.name');
$password_is_optional = config('usercare.enable.optional.password');

if ($password_is_optional) {
Schema::table($table, function (Blueprint $table) {
$table->string('password')->nullable()->change();
});
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
6 changes: 3 additions & 3 deletions src/Http/Requests/CreateUserAccountRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public function authorize(): bool
public function rules(): array
{
$users_table = config('usercare.user.table.name');
$email_is_optional = config('usercare.user.email_is_optional');
$email_is_optional = config('usercare.enable.optional.email');

$proper_names_rules = (proper_names_is_enabled_and_exists())
? ['last_name' => 'required|string', 'first_name' => 'required|string']
? ['last_name' => 'string', 'first_name' => 'string']
: [];

$email_rules = ($email_is_optional)
Expand All @@ -50,7 +50,7 @@ public function rules(): array
return array_merge([
'name' => 'string|unique:'.$users_table,
'gender' => 'string',
'password' => 'required|string|min:6|confirmed',
'password' => 'string|min:6|confirmed',
], $email_rules, $phone_number_rules, $proper_names_rules, $account_type_rules);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Requests/UpdateUserAccountRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function rules(): array
$users_table = config('usercare.user.table.name');
$users_table_pk = config('usercare.user.table.primary_key');

$email_is_optional = config('usercare.user.email_is_optional');
$email_is_optional = config('usercare.enable.optional.email');

$proper_names_rules = (proper_names_is_enabled_and_exists())
? ['last_name' => 'required|string', 'first_name' => 'required|string']
Expand Down
29 changes: 22 additions & 7 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ function generate_random_username(int $min = 3, int $max = 12, $rand_bytes = tru
* @return bool
*/
function account_type_is_enabled_and_exists(): bool {
$account_types_enable = config('userly.account_type.enable');
$account_types_table = config('userly.account_type.table.name');
$account_types_enable = config('usercare.account_type.enable');
$account_types_table = config('usercare.account_type.table.name');

return ($account_types_enable && Schema::hasTable($account_types_table))
? true
Expand All @@ -107,22 +107,37 @@ function account_type_is_enabled_and_exists(): bool {
* @return bool
*/
function schema_has_proper_names_columns(): bool {
$table = config('userly.user.table.name');
$table = config('usercare.user.table.name');

return (Schema::hasColumn($table, 'last_name') && Schema::hasColumn($table, 'first_name'))
? true
: false;
}
}

if (!function_exists('proper_names_is_enabled_and_does_not_exist')) {
/**
* Check if proper names is enabled and exists in schema.
*
* @return bool
*/
function proper_names_is_enabled_and_does_not_exist(): bool {
$proper_names_enabled = config('usercare.enable.proper_names');

return ($proper_names_enabled && !schema_has_proper_names_columns())
? true
: false;
}
}

if (!function_exists('proper_names_is_enabled_and_exists')) {
/**
* Check if proper names is enabled and exists in schema.
*
* @return bool
*/
function proper_names_is_enabled_and_exists(): bool {
$proper_names_enabled = config('userly.enable.proper_names');
$proper_names_enabled = config('usercare.enable.proper_names');

return ($proper_names_enabled && schema_has_proper_names_columns())
? true
Expand All @@ -137,7 +152,7 @@ function proper_names_is_enabled_and_exists(): bool {
* @return bool
*/
function schema_has_phone_number_column(): bool {
$table = config('userly.user.table.name');
$table = config('usercare.user.table.name');

return (Schema::hasColumn($table, 'phone_number'))
? true
Expand All @@ -152,7 +167,7 @@ function schema_has_phone_number_column(): bool {
* @return bool
*/
function phone_number_is_enabled_and_exists(): bool {
$phone_number_enabled = config('userly.enable.phone_number');
$phone_number_enabled = config('usercare.enable.phone_number');

return ($phone_number_enabled && schema_has_phone_number_column())
? true
Expand All @@ -167,7 +182,7 @@ function phone_number_is_enabled_and_exists(): bool {
* @return bool
*/
function schema_has_gender_column(): bool {
$table = config('userly.user.table.name');
$table = config('usercare.user.table.name');

return (Schema::hasColumn($table, 'gender'))
? true
Expand Down

0 comments on commit 5404b61

Please sign in to comment.