Skip to content

Commit

Permalink
Merge pull request #776 from portabilis/portabilis-patch-2021-08-02
Browse files Browse the repository at this point in the history
[2.6] Portabilis patch 02/08/2021
  • Loading branch information
edersoares authored Aug 11, 2021
2 parents 4608ed6 + 6a01ccd commit b8bf76f
Show file tree
Hide file tree
Showing 87 changed files with 2,257 additions and 1,424 deletions.
47 changes: 47 additions & 0 deletions app/Console/Commands/DisableUsersWithAccessExpired.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Console\Commands;

use App\Jobs\BatchDisableUsersWithDaysGoneSinceLastAccess;
use Illuminate\Console\Command;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Support\Facades\DB;

class DisableUsersWithAccessExpired extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'disable:users-with-access-expired';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Disable users with access expired';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$job = new BatchDisableUsersWithDaysGoneSinceLastAccess(DB::getDefaultConnection());
app(Dispatcher::class)->dispatch($job);
return 0;
}
}
13 changes: 10 additions & 3 deletions app/Http/Controllers/Auth/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Services\ChangeUserPasswordService;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;

Expand Down Expand Up @@ -33,8 +34,9 @@ class ResetPasswordController extends Controller
*
* @return void
*/
public function __construct()
public function __construct(ChangeUserPasswordService $changeUserPasswordService)
{
$this->changeUserPasswordService = $changeUserPasswordService;
$this->middleware('guest');
}

Expand All @@ -46,7 +48,7 @@ protected function rules()
return [
'token' => 'required',
'login' => 'required',
'password' => 'required|confirmed|min:6',
'password' => 'required|confirmed',
];
}

Expand All @@ -58,7 +60,6 @@ protected function validationErrorMessages()
return [
'password.required' => 'O campo senha é obrigatório.',
'password.confirmed' => 'As senhas não são iguais.',
'password.min' => 'A senha deve conter ao menos 8 caracteres.',
];
}

Expand All @@ -74,4 +75,10 @@ protected function credentials(Request $request)
'token'
);
}

protected function setUserPassword($user, $password)
{
$employee = $user->employee;
$this->changeUserPasswordService->execute($employee, $password);
}
}
18 changes: 13 additions & 5 deletions app/Http/Controllers/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Services\ChangeUserPasswordService;
use App\User;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
Expand All @@ -11,6 +12,11 @@ class PasswordController extends Controller
{
use ResetsPasswords;

public function __construct(ChangeUserPasswordService $changeUserPasswordService)
{
$this->changeUserPasswordService = $changeUserPasswordService;
}

public function change(Request $request, User $user)
{
if ($request->isMethod('get')) {
Expand All @@ -30,10 +36,6 @@ function ($user, $password) {
);

if ($response == Password::PASSWORD_RESET) {
$employee = $user->employee;
$employee->force_reset_password = false;
$employee->save();

return $this->sendResetResponse($request, $response);
}

Expand All @@ -44,7 +46,7 @@ protected function rules()
{
return [
'login' => 'required',
'password' => 'required|confirmed|min:8',
'password' => 'required|confirmed',
];
}

Expand Down Expand Up @@ -77,4 +79,10 @@ protected function credentials(Request $request)
'token'
);
}

protected function setUserPassword($user, $password)
{
$employee = $user->employee;
$this->changeUserPasswordService->execute($employee, $password);
}
}
14 changes: 14 additions & 0 deletions app/Http/Middleware/CheckResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

namespace App\Http\Middleware;

use App\Services\ForceUserChangePasswordService;
use Closure;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CheckResetPassword
{
public function __construct(ForceUserChangePasswordService $forceUserChangePasswordService)
{
$this->forceUserChangePasswordService = $forceUserChangePasswordService;
}

/**
* Handle an incoming request.
*
Expand All @@ -24,10 +31,17 @@ public function handle($request, Closure $next)
return $next($request);
}

$this->validateUserExpirationPassword($user);

if ($user->employee->force_reset_password) {
return redirect()->route('change-password');
}

return $next($request);
}

public function validateUserExpirationPassword(Authenticatable $user)
{
$this->forceUserChangePasswordService->execute($user);
}
}
49 changes: 49 additions & 0 deletions app/Jobs/BatchDisableUsersWithDaysGoneSinceLastAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Jobs;

use App\Services\DisableUsersWithDaysGoneSinceLastAccessService;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

class BatchDisableUsersWithDaysGoneSinceLastAccess implements ShouldQueue, ShouldBeUniqueUntilProcessing
{
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;

private $databaseConnection;

public function __construct($databaseConnection)
{
$this->databaseConnection = $databaseConnection;
}

public function handle(Repository $config)
{
DB::setDefaultConnection($this->databaseConnection);
$expirationPeriod = $config->get('legacy.app.user_accounts.max_days_without_login_to_disable_user');

if (empty($expirationPeriod) === false) {
$this->disableUsersWithDaysGoneSinceLastAccess($expirationPeriod);
}
}

private function disableUsersWithDaysGoneSinceLastAccess($expirationPeriod): void
{
$users = (new User())->getActiveUsersNotAdmin();

foreach ($users as $user) {
$disableUsersWithDaysGoneSinceLastAccessService = app(DisableUsersWithDaysGoneSinceLastAccessService::class);
$disableUsersWithDaysGoneSinceLastAccessService->execute($user);
}
}
}
18 changes: 17 additions & 1 deletion app/Listeners/AuthenticatedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

namespace App\Listeners;

use App\Services\DisableUsersWithDaysGoneSinceLastAccessService;
use Illuminate\Auth\Events\Authenticated;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class AuthenticatedUser
{
private $disableUsersWithDaysGoneSinceLastAccessService;

public function __construct(DisableUsersWithDaysGoneSinceLastAccessService $disableUsersWithDaysGoneSinceLastAccessService)
{
$this->disableUsersWithDaysGoneSinceLastAccessService = $disableUsersWithDaysGoneSinceLastAccessService;
}

/**
* Handle the event.
*
Expand All @@ -25,12 +34,19 @@ public function handle(Authenticated $event)
]);
}

$this->validateUserExpirationPeriod($event->user);

if ($event->user->isExpired()) {
Auth::logout();

throw ValidationException::withMessages([
$event->user->login => __('auth.inactive')
$event->user->login => __('auth.expired')
]);
}
}

public function validateUserExpirationPeriod(Authenticatable $user)
{
$this->disableUsersWithDaysGoneSinceLastAccessService->execute($user);
}
}
1 change: 1 addition & 0 deletions app/Models/Exporter/Student.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function getExportedColumnsByGroup()
'grade' => 'Série',
'course' => 'Curso',
'registration_date' => 'Data da Matrícula',
'registration_out' => 'Data de saída da matrícula',
'year' => 'Ano',
'status_text' => 'Situação da Matrícula',
'period' => 'Turno',
Expand Down
32 changes: 32 additions & 0 deletions app/Models/LegacyAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class LegacyAccess extends Model
{
/**
* @var string
*/
protected $table = 'portal.acesso';

/**
* @var string
*/
protected $primaryKey = 'cod_acesso';

/**
* @var bool
*/
public $timestamps = false;

protected $dates = ['data_hora'];

public function getLastAccess()
{
return $this->query()
->orderBy('data_hora', 'DESC')
->first();
}
}
13 changes: 13 additions & 0 deletions app/Models/LegacyEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
Expand Down Expand Up @@ -39,6 +40,8 @@ class LegacyEmployee extends Model
'email',
];

protected $dates = ['data_reativa_conta', 'data_troca_senha'];

/**
* @return string
*/
Expand Down Expand Up @@ -106,4 +109,14 @@ public function getActiveAttribute()
{
return boolval($this->ativo);
}

public function getEnabledUserDate(): ?Carbon
{
return $this->data_reativa_conta;
}

public function getPasswordUpdatedDate(): ?Carbon
{
return $this->data_troca_senha;
}
}
35 changes: 35 additions & 0 deletions app/Services/ChangeUserPasswordService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Services;

use App\Models\LegacyEmployee;
use Carbon\Carbon;
use Illuminate\Contracts\Hashing\Hasher;

class ChangeUserPasswordService
{
private $validateUserPasswordService;
private $hash;
private $carbon;

public function __construct(ValidateUserPasswordService $validateUserPasswordService, Hasher $hash, Carbon $carbon)
{
$this->validateUserPasswordService = $validateUserPasswordService;
$this->hash = $hash;
$this->carbon = $carbon;
}

public function execute(LegacyEmployee $legacyEmployee, string $password)
{
$this->validate($password, $legacyEmployee->getPasswordAttribute());
$legacyEmployee->setPasswordAttribute($this->hash->make($password));
$legacyEmployee->force_reset_password = false;
$legacyEmployee->data_troca_senha = $this->carbon->nowWithSameTz();
$legacyEmployee->save();
}

public function validate(string $newPassword, string $oldPassword)
{
$this->validateUserPasswordService->execute($newPassword, $oldPassword);
}
}
Loading

0 comments on commit b8bf76f

Please sign in to comment.