From 300ff70130ea50149d9ae00548bf055cd5789a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20Cesar=20Laura=20Avenda=C3=B1o?= Date: Mon, 14 Dec 2020 20:49:52 +0000 Subject: [PATCH] PMCORE-2540 --- .../src/ProcessMaker/Model/UserTest.php | 44 +++++++++ .../engine/src/ProcessMaker/Model/User.php | 95 +++++++++++++++++++ .../src/ProcessMaker/Services/Api/Home.php | 26 +++++ 3 files changed, 165 insertions(+) diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/UserTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/UserTest.php index 50b164e5a..fd7d6e75f 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/UserTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/UserTest.php @@ -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)); + } } \ No newline at end of file diff --git a/workflow/engine/src/ProcessMaker/Model/User.php b/workflow/engine/src/ProcessMaker/Model/User.php index fadc8104c..2d90d3469 100644 --- a/workflow/engine/src/ProcessMaker/Model/User.php +++ b/workflow/engine/src/ProcessMaker/Model/User.php @@ -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()}."); + } + } } diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Home.php b/workflow/engine/src/ProcessMaker/Services/Api/Home.php index ca9339c51..2f7032a64 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/Home.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/Home.php @@ -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 *