Compare commits

...

2 Commits

Author SHA1 Message Date
Maxence Lange a969cfad8a syntactic migration
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
2023-08-18 10:17:48 -01:00
Maxence Lange 40584f1dee admin have no special rights on users' entries
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
2023-08-18 10:13:11 -01:00
2 changed files with 30 additions and 66 deletions
@@ -1,10 +1,14 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author Martin Mattel <martin.mattel@diemattels.at>
* @author Maxence Lange <maxence@artificial-owl.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Robin McCorkell <robin@mccorkell.me.uk>
@@ -26,52 +30,32 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Controller;
use OCA\Files_External\Lib\Auth\Password\GlobalAuth;
use OCA\Files_External\Lib\Auth\PublicKey\RSA;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUserSession;
class AjaxController extends Controller {
/** @var RSA */
private $rsaMechanism;
/** @var GlobalAuth */
private $globalAuth;
/** @var IUserSession */
private $userSession;
/** @var IGroupManager */
private $groupManager;
/**
* @param string $appName
* @param IRequest $request
* @param RSA $rsaMechanism
* @param GlobalAuth $globalAuth
* @param IUserSession $userSession
* @param IGroupManager $groupManager
*/
public function __construct($appName,
IRequest $request,
RSA $rsaMechanism,
GlobalAuth $globalAuth,
IUserSession $userSession,
IGroupManager $groupManager) {
public function __construct(
string $appName,
IRequest $request,
private RSA $rsaMechanism,
private GlobalAuth $globalAuth,
private IUserSession $userSession
) {
parent::__construct($appName, $request);
$this->rsaMechanism = $rsaMechanism;
$this->globalAuth = $globalAuth;
$this->userSession = $userSession;
$this->groupManager = $groupManager;
}
/**
* @param int $keyLength
* @return array
*/
private function generateSshKeys($keyLength) {
private function generateSshKeys(int $keyLength): array {
$key = $this->rsaMechanism->createKey($keyLength);
// Replace the placeholder label with a more meaningful one
$key['publickey'] = str_replace('phpseclib-generated-key', gethostname(), $key['publickey']);
@@ -83,9 +67,11 @@ class AjaxController extends Controller {
* Generates an SSH public/private key pair.
*
* @NoAdminRequired
*
* @param int $keyLength
* @return JSONResponse
*/
public function getSshKeys($keyLength = 1024) {
public function getSshKeys(int $keyLength = 1024): JSONResponse {
$key = $this->generateSshKeys($keyLength);
return new JSONResponse(
['data' => [
@@ -104,17 +90,13 @@ class AjaxController extends Controller {
* @param string $password
* @return bool
*/
public function saveGlobalCredentials($uid, $user, $password) {
$currentUser = $this->userSession->getUser();
// Non-admins can only edit their own credentials
$allowedToEdit = ($this->groupManager->isAdmin($currentUser->getUID()) || $currentUser->getUID() === $uid);
if ($allowedToEdit) {
$this->globalAuth->saveAuth($uid, $user, $password);
return true;
} else {
public function saveGlobalCredentials(string $uid, string $user, string $password): bool {
if ($this->userSession->getUser()->getUID() !== $uid) {
return false;
}
$this->globalAuth->saveAuth($uid, $user, $password);
return true;
}
}
@@ -102,17 +102,11 @@ class AjaxControllerTest extends TestCase {
->expects($this->once())
->method('getUser')
->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('MyAdminUid')
->willReturn(true);
$this->globalAuth
->expects($this->once())
->method('saveAuth')
->with('UidOfTestUser', 'test', 'password');
->expects($this->never())
->method('saveAuth');
$this->assertSame(true, $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password'));
$this->assertSame(false, $this->ajaxController->saveGlobalCredentials('UidOfTestUser', 'test', 'password'));
}
public function testSaveGlobalCredentialsAsAdminForSelf() {
@@ -125,11 +119,6 @@ class AjaxControllerTest extends TestCase {
->expects($this->once())
->method('getUser')
->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('MyAdminUid')
->willReturn(true);
$this->globalAuth
->expects($this->once())
->method('saveAuth')
@@ -141,18 +130,13 @@ class AjaxControllerTest extends TestCase {
public function testSaveGlobalCredentialsAsNormalUserForSelf() {
$user = $this->createMock(IUser::class);
$user
->expects($this->exactly(2))
->expects($this->once())
->method('getUID')
->willReturn('MyUserUid');
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('MyUserUid')
->willReturn(false);
$this->globalAuth
->expects($this->once())
->method('saveAuth')
@@ -164,18 +148,16 @@ class AjaxControllerTest extends TestCase {
public function testSaveGlobalCredentialsAsNormalUserForAnotherUser() {
$user = $this->createMock(IUser::class);
$user
->expects($this->exactly(2))
->expects($this->once())
->method('getUID')
->willReturn('MyUserUid');
$this->userSession
->expects($this->once())
->method('getUser')
->willReturn($user);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->with('MyUserUid')
->willReturn(false);
$this->globalAuth
->expects($this->never())
->method('saveAuth');
$this->assertSame(false, $this->ajaxController->saveGlobalCredentials('AnotherUserUid', 'test', 'password'));
}