PMCORE-2540

This commit is contained in:
Julio Cesar Laura Avendaño
2020-12-14 20:49:52 +00:00
parent 81677b9bb1
commit 300ff70130
3 changed files with 165 additions and 0 deletions

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()}.");
}
}
}