This commit is contained in:
Andrea Adamczyk
2019-07-05 16:00:43 -04:00
parent a07867510d
commit 448e0722fa
7 changed files with 196 additions and 8 deletions

View File

@@ -1,7 +1,8 @@
<?php
use Faker\Generator as Faker;
$factory->define(\ProcessMaker\Model\User::class, function(Faker $faker) {
$factory->define(\ProcessMaker\Model\User::class, function (Faker $faker) {
return [
'USR_UID' => G::generateUniqueID(),
'USR_USERNAME' => $faker->unique()->userName,
@@ -9,11 +10,12 @@ $factory->define(\ProcessMaker\Model\User::class, function(Faker $faker) {
'USR_FIRSTNAME' => $faker->firstName,
'USR_LASTNAME' => $faker->lastName,
'USR_EMAIL' => $faker->unique()->email,
'USR_DUE_DATE' => new \Carbon\Carbon(2030,1,1),
'USR_DUE_DATE' => new \Carbon\Carbon(2030, 1, 1),
'USR_STATUS' => 'ACTIVE',
'USR_ROLE' => $faker->randomElement(['PROCESSMAKER_ADMIN', 'PROCESSMAKER_OPERATOR']),
'USR_UX' => 'NORMAL',
'USR_TIME_ZONE' => 'America/Anguilla',
'USR_DEFAULT_LANG' => 'en',
'USR_LAST_LOGIN' => new \Carbon\Carbon(2019, 1, 1)
];
});

View File

@@ -0,0 +1,74 @@
<?php
namespace Tests\unit\workflow\engine\src\ProcessMaker\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use ProcessMaker\Model\User;
use Tests\TestCase;
class UserTest extends TestCase
{
use DatabaseTransactions;
/**
* Tests the users filters scope with the usr uid filter
*
* @test
*/
public function it_should_test_the_users_filters_scope_with_usr_uid()
{
$user = factory(User::class, 4)->create();
$filters = ['USR_UID' => $user[0]['USR_UID']];
$userQuery = User::query()->select();
$userQuery->userFilters($filters);
$result = $userQuery->get()->values()->toArray();
// Assert the expected numbers of rows in the result
$this->assertCount(1, $result);
// Assert the filter has been set successful
$this->assertEquals($user[0]['USR_UID'], $result[0]['USR_UID']);
$this->assertNotEquals($user[1]['USR_UID'], $result[0]['USR_UID']);
}
/**
* Tests the users filters scope with the usr id filter
*
* @test
*/
public function it_should_test_the_users_filters_scope_with_usr_id()
{
$user = factory(User::class, 4)->create();
$filters = ['USR_ID' => $user[0]['USR_ID']];
$userQuery = User::query()->select();
$userQuery->userFilters($filters);
$result = $userQuery->get()->values()->toArray();
// Assert the expected numbers of rows in the result
$this->assertCount(1, $result);
// Assert the filter has been set successful
$this->assertEquals($user[0]['USR_ID'], $result[0]['USR_ID']);
$this->assertNotEquals($user[1]['USR_ID'], $result[0]['USR_ID']);
}
/**
* Tests the exception in the users filters scope
*
* @test
*/
public function it_should_test_the_exception_in_users_filters_scope()
{
factory(User::class, 4)->create();
$filters = [];
$userQuery = User::query()->select();
//Expect an exception message
$this->expectExceptionMessage("There are no filter for loading a user model");
//Call the userFilters scope
$userQuery->userFilters($filters);
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Tests\unit\workflow\engine\src\ProcessMaker\Util\Helpers;
use ProcessMaker\Model\User;
use Tests\TestCase;
class UpdateUserLastLoginTest extends TestCase
{
/**
* It tests the updateUserLastLogin function
*
* @test
*/
public function it_should_test_the_update_last_login_date_function_when_it_does_not_fail()
{
$user = factory(User::class)->create();
$userLog = ['USR_UID' => $user['USR_UID'], 'LOG_INIT_DATE' => date('Y-m-d H:i:s')];
// Call the updateUserLastLogin function
$result = updateUserLastLogin($userLog);
// Asserts the update has been successful
$this->assertEquals(1, $result);
}
/**
* It tests the updateUserLastLogin function exception
*
* @test
*/
public function it_should_test_the_update_last_login_date_function_when_it_fails()
{
$user = factory(User::class)->create();
$userLog = ['USR_UID' => $user['USR_UID']];
// Assert the expected exception
$this->expectExceptionMessage("Undefined index: LOG_INIT_DATE");
// Call the updateUserLastLogin function
updateUserLastLogin($userLog);
$userLog = null;
// Assert the expected exception
$this->expectExceptionMessage("There are no filter for loading a user model");
// Call the updateUserLastLogin function
updateUserLastLogin($userLog);
$userLog = '';
// Assert the expected exception
$this->expectExceptionMessage("Illegal string offset 'USR_UID'");
// Call the updateUserLastLogin function
updateUserLastLogin($userLog);
}
}

View File

@@ -326,6 +326,8 @@ try {
}
if ($RBAC->singleSignOn) {
// Update the User's last login date
updateUserLastLogin($aLog);
G::header('Location: ' . $sLocation);
die();
}
@@ -336,6 +338,8 @@ try {
if ($changePassword === true) {
$user = new User();
$currentUser = $user->changePassword($_SESSION['USER_LOGGED'], $_POST['form']['USR_PASSWORD']);
// Update the User's last login date
updateUserLastLogin($aLog);
G::header('Location: ' . $currentUser["__REDIRECT_PATH__"]);
return;
}
@@ -394,6 +398,9 @@ try {
setcookie("PM-TabPrimary", 101010010, time() + (24 * 60 * 60), '/');
}
// Update the User's last login date
updateUserLastLogin($aLog);
$oPluginRegistry = PluginRegistry::loadSingleton();
if ($oPluginRegistry->existsTrigger ( PM_AFTER_LOGIN )) {
$oPluginRegistry->executeTriggers ( PM_AFTER_LOGIN , $_SESSION['USER_LOGGED'] );

View File

@@ -116,12 +116,6 @@ if (isset($_SESSION['USER_LOGGED'])) {
$aLog['USR_UID'] = $aRow['USR_UID'];
$weblog->update($aLog);
$aLog = array();
$aLog['USR_UID'] = $aRow['USR_UID'];
$aLog['USR_LAST_LOGIN'] = $endDate;
$user = new Users();
$aUser = $user->update($aLog);
}
}
} else {

View File

@@ -3,6 +3,7 @@
namespace ProcessMaker\Model;
use Illuminate\Database\Eloquent\Model;
use Exception;
class User extends Model
{
@@ -39,4 +40,26 @@ class User extends Model
{
return User::find($usrUid)->groups()->get();
}
/**
* Scope for the specified user
*
* @param \Illuminate\Database\Eloquent\Builder $query @param \Illuminate\Database\Eloquent\Builder $query
* @param array $filters
*
* @return \Illuminate\Database\Eloquent\Builder
* @throws Exception
*/
public function scopeUserFilters($query, array $filters)
{
if (!empty($filters['USR_ID'])) {
$query->where('USR_ID', $filters['USR_ID']);
} elseif (!empty($filters['USR_UID'])) {
$query->where('USR_UID', $filters['USR_UID']);
} else {
throw new Exception("There are no filter for loading a user model");
}
return $query;
}
}

View File

@@ -2,6 +2,7 @@
use Illuminate\Session\TokenMismatchException;
use Illuminate\Support\Str;
use ProcessMaker\Model\User;
/**
* We will send a case note in the actions by email
@@ -493,3 +494,29 @@ if (!function_exists('set_magic_quotes_runtime')) {
return false;
}
}
/**
* Update the USER table with the last login date
*
* @param array $userLog
* @return int
* @throws Exception
*
* @see workflow/engine/methods/login/authentication.php
*/
function updateUserLastLogin($userLog, $keyLastLogin = 'LOG_INIT_DATE')
{
try {
$filters = [];
$filters['USR_UID'] = $userLog['USR_UID'];
$user = User::query();
$user->userFilters($filters);
$res = $user->update(['USR_LAST_LOGIN' => $userLog[$keyLastLogin]]);
return $res;
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}