Merged in feature/PMCORE-2540 (pull request #7616)

PMCORE-2540

Approved-by: Paula Quispe <paula.quispe@processmaker.com>
This commit is contained in:
Julio Cesar Laura Avendaño
2020-12-15 14:43:13 +00:00
3 changed files with 165 additions and 0 deletions

View File

@@ -71,4 +71,48 @@ class UserTest extends TestCase
//Call the userFilters scope
$userQuery->userFilters($filters);
}
/**
* It test get users for the new home view
*
* @covers \ProcessMaker\Model\User::getUsersForHome()
* @test
*/
public function it_should_test_get_users_for_home()
{
// Create five users (3 active, 1 on vacation, 1 inactive)
factory(User::class)->create([
'USR_USERNAME' => 'jsmith',
'USR_FIRSTNAME' => 'John',
'USR_LASTNAME' => 'Smith',
]);
factory(User::class)->create([
'USR_USERNAME' => 'asmith',
'USR_FIRSTNAME' => 'Adam',
'USR_LASTNAME' => 'Smith',
]);
factory(User::class)->create([
'USR_USERNAME' => 'wsmith',
'USR_FIRSTNAME' => 'Will',
'USR_LASTNAME' => 'Smith',
]);
factory(User::class)->create([
'USR_USERNAME' => 'wwallace',
'USR_FIRSTNAME' => 'Williams',
'USR_LASTNAME' => 'Wallace',
'USR_STATUS' => 'VACATION',
]);
factory(User::class)->create([
'USR_USERNAME' => 'msmith',
'USR_FIRSTNAME' => 'Marcus',
'USR_LASTNAME' => 'Smith',
'USR_STATUS' => 'INACTIVE',
]);
// Assertions
$this->assertCount(4, User::getUsersForHome());
$this->assertCount(3, User::getUsersForHome('Smith'));
$this->assertCount(4, User::getUsersForHome(null, null, 2));
$this->assertCount(1, User::getUsersForHome(null, 2, 1));
}
}

View File

@@ -2,8 +2,10 @@
namespace ProcessMaker\Model;
use Configurations;
use Illuminate\Database\Eloquent\Model;
use Exception;
use RBAC;
class User extends Model
{
@@ -62,4 +64,97 @@ class User extends Model
return $query;
}
/**
* Scope a query to exclude the guest user
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeWithoutGuest($query)
{
$query->where('USR_UID', '<>', RBAC::GUEST_USER_UID);
}
/**
* Scope a query to include only active users (ACTIVE, VACATION)
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeActive($query)
{
return $query->where('USERS.USR_STATUS', ['ACTIVE', 'VACATION']);
}
/**
* Get all users, paged optionally, can be sent a text to filter results by user information (first name, last name, username)
*
* @param string $text
* @param int $offset
* @param int $limit
*
* @return array
*
* @throws Exception
*/
public static function getUsersForHome($text = null, $offset = null, $limit = null)
{
try {
// Load configurations of the environment
$configurations = new Configurations();
// Field to order the results
$orderBy = $configurations->userNameFormatGetFirstFieldByUsersTable();
// Format of the user names
$formatName = $configurations->getFormats()['format'];
// Get users from the current workspace
$query = User::query()->select(['USR_ID', 'USR_USERNAME', 'USR_FIRSTNAME', 'USR_LASTNAME']);
// Set full name condition if is sent
if (!empty($text)) {
$query->where(function ($query) use ($text) {
$query->orWhere('USR_USERNAME', 'LIKE', "%{$text}%");
$query->orWhere('USR_FIRSTNAME', 'LIKE', "%{$text}%");
$query->orWhere('USR_LASTNAME', 'LIKE', "%{$text}%");
});
}
// Exclude guest user
$query->withoutGuest();
// Only get active
$query->active();
// Order by full name
$query->orderBy($orderBy);
// Set pagination if offset and limit are sent
if (!is_null($offset) && !is_null($limit)) {
$query->offset($offset);
$query->limit($limit);
}
// Get users
$users = $query->get()->toArray();
// Populate the field with the user names in format
$users = array_map(function ($user) use ($configurations, $formatName) {
// Format the user names
$user['USR_FULLNAME'] = $configurations->usersNameFormatBySetParameters($formatName,
$user['USR_USERNAME'], $user['USR_FIRSTNAME'], $user['USR_LASTNAME']);
// Return value with the new element
return $user;
}, $users);
// Return users
return $users;
} catch (Exception $e) {
throw new Exception("Error getting the users: {$e->getMessage()}.");
}
}
}

View File

@@ -599,6 +599,32 @@ class Home extends Api
}
}
/**
* Get all users, paged optionally, can be sent a text to filter results by user information (first name, last name, username)
*
* @url GET /users
*
* @param string $text
* @param int $offset
* @param int $limit
*
* @return array
*
* @throws Exception
*
* @access protected
* @class AccessControl {@permission PM_CASES}
*/
public function getUsers($text = null, $offset = null, $limit = null)
{
try {
$users = User::getUsersForHome($text, $offset, $limit);
return $users;
} catch (Exception $e) {
throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage());
}
}
/**
* Get the tasks counters for todo, draft, paused and unassigned
*