Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[user] Add getSites functions in User class #9102

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion modules/user_accounts/php/user_accounts.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,17 @@ class User_Accounts extends \DataFrameworkMenu
'Y' => 'Yes',
'N' => 'No',
];
// Get user
$factory = \NDB_Factory::singleton();
$user = $factory->user();

// Without user_acounts_multisite permission, only show user sites
$sites = $user->hasPermission('user_accounts_multisite') ?
\Utility::getSiteList(false)
: $user->getSiteNamesList();

return [
'sites' => \Utility::getSiteList(false),
'sites' => $sites,
'projects' => \Utility::getProjectList(),
'actives' => $yesNoOptions,
'pendingApprovals' => $yesNoOptions,
Expand Down
15 changes: 14 additions & 1 deletion modules/user_accounts/php/useraccountrowprovisioner.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ class UserAccountRowProvisioner extends \LORIS\Data\Provisioners\DBRowProvisione
*/
function __construct()
{
// Get user
$factory = \NDB_Factory::singleton();
$user = $factory->user();
if (!$user->hasPermission('user_accounts_multisite')) {
$siteLimitQuery = "LEFT JOIN user_psc_rel upsr2
ON (upsr2.CenterID=upsr.CenterID)
WHERE upsr2.UserID=:uid";
$params = ['uid' => $user->getId()];
} else {
$siteLimitQuery = '';
$params = [];
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is necessary.

user_accounts.class.inc already has:

    /**
     * Tells the base class that this page's provisioner can support the
     * HasAnyPermissionOrUserSiteMatch filter.
     *
     * @return ?array of permissions or null
     */
    public function allSitePermissionNames() : ?array
    {
        return ['user_accounts_multisite'];
    }

so shouldn't the HasAnyPermissionOrUserSiteMatch filter take care of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allSitePermissionNames function takes care of filtering out users who don't belong to one of the user's sites, but this code makes it so that only the user's sites are shown in the "Site" column of the table.
image

With the change from user_accounts.class.inc but without this change, there were empty strings in between commas:
image

parent::__construct(
"SELECT GROUP_CONCAT(DISTINCT upsr.CenterID) as centerIds,
GROUP_CONCAT(DISTINCT uprr.ProjectID) as projectIds,
Expand All @@ -29,9 +41,10 @@ class UserAccountRowProvisioner extends \LORIS\Data\Provisioners\DBRowProvisione
FROM users u
LEFT JOIN user_psc_rel upsr ON (upsr.UserID=u.ID)
LEFT JOIN user_project_rel uprr ON (uprr.UserID=u.ID)
$siteLimitQuery
GROUP BY u.ID
ORDER BY u.UserID",
[]
$params
);
}

Expand Down
4 changes: 2 additions & 2 deletions modules/user_accounts/test/TestPlan.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ When creating or editing a user: (subtest: edit_user)
13. Check that if generating a new password for a user an email is sent to that user containing the new password (requires
email server).
14. Check that when editing a user account it is not possible to set the password to its actual value (i.e. it needs to change). [Automated]
15. Verify that if the editor does not have permission 'Across all sites add and edit users' then the site drop-down list is populated with
the editor's associated sites, otherwise all sites are displayed.
15. Verify that if the editor does not have permission 'User Accounts: View/Create/Edit User Accounts - All Sites' then the multi-select
site field in both edit user page and the filters for the data table are populated with the editor's associated sites, otherwise all sites are displayed.
16. Check that if the 'Additional user information' entry is set to false in the Configuration module, fields Degree,
Academic Position, Institution, Department, Street Address, City, State/Province, Country and FAX are not shown.
17. Check that the 'Examiner At:' and 'Examiner Status' sections are available only if you have the 'Across all sites add and certify examiners'.
Expand Down
16 changes: 16 additions & 0 deletions modules/user_accounts/test/user_accountsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public function setUp(): void
{
parent::setUp();
$password = new \Password($this->validPassword);

$permID = $this->DB->pselectOne(
"SELECT permID FROM permissions WHERE code='user_accounts_multisite'",
[]
);

$this->DB->insert(
"users",
[
Expand Down Expand Up @@ -93,6 +99,15 @@ public function setUp(): void
'ProjectID' => 1,
]
);

$this->DB->insert(
"user_perm_rel",
[
'userID' => 999995,
'permID' => $permID,
]
);

}

/**
Expand Down Expand Up @@ -506,6 +521,7 @@ function tearDown(): void
$this->DB->delete("user_psc_rel", ["UserID" => 999995]);
$this->DB->delete("user_project_rel", ["UserID" => 999995]);
$this->DB->delete("users", ["UserID" => 'UnitTesterTwo']);
$this->DB->delete("user_perm_rel", ["userID" => 999995]);
parent::tearDown();
}
}
Expand Down
31 changes: 31 additions & 0 deletions php/libraries/User.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,37 @@ class User extends UserPermissions implements
return $site_list;
}

/**
* Returns all user's sites
*
* @return array
*/
function getSites(): array
{
return array_map(
function ($centerID) {
return \Site::singleton($centerID);
},
$this->getCenterIDs()
);
}

/**
* Returns all user's sites in associative
* array (CenterID => CenterName)
*
* @return array
*/
function getSiteNamesList(): array
{
$sites = [];
foreach ($this->getSites() as $site) {
$sites[$site->getCenterID()->__toString()] = $site->getCenterName();
}
return $sites;
}


/**
* Returns all user's sites that are StudySites
*
Expand Down
Loading