]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use the new method in the old one to remove duplicate code 4522/head
authorJoas Schilling <coding@schilljs.com>
Thu, 27 Apr 2017 06:56:51 +0000 (08:56 +0200)
committerJoas Schilling <coding@schilljs.com>
Thu, 27 Apr 2017 06:56:51 +0000 (08:56 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/User/Manager.php
lib/public/IUserManager.php
tests/lib/User/ManagerTest.php

index d482ee811f76db0b1d3daa1237cc8a4dccdbb74c..4a87dc7161c32cdb0e1cb3f80787c4103cc77f80 100644 (file)
@@ -280,59 +280,57 @@ class Manager extends PublicEmitter implements IUserManager {
        /**
         * @param string $uid
         * @param string $password
-        * @throws \Exception
-        * @return bool|\OC\User\User the created user or false
+        * @throws \InvalidArgumentException
+        * @return bool|IUser the created user or false
         */
        public function createUser($uid, $password) {
+               foreach ($this->backends as $backend) {
+                       if ($backend->implementsActions(Backend::CREATE_USER)) {
+                               return $this->createUserFromBackend($uid, $password, $backend);
+                       }
+               }
+
+               return false;
+       }
+
+       /**
+        * @param string $uid
+        * @param string $password
+        * @param UserInterface $backend
+        * @return IUser|null
+        * @throws \InvalidArgumentException
+        */
+       public function createUserFromBackend($uid, $password, UserInterface $backend) {
                $l = \OC::$server->getL10N('lib');
+
                // Check the name for bad characters
                // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'"
                if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) {
-                       throw new \Exception($l->t('Only the following characters are allowed in a username:'
+                       throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:'
                                . ' "a-z", "A-Z", "0-9", and "_.@-\'"'));
                }
                // No empty username
-               if (trim($uid) == '') {
-                       throw new \Exception($l->t('A valid username must be provided'));
+               if (trim($uid) === '') {
+                       throw new \InvalidArgumentException($l->t('A valid username must be provided'));
                }
                // No whitespace at the beginning or at the end
                if (trim($uid) !== $uid) {
-                       throw new \Exception($l->t('Username contains whitespace at the beginning or at the end'));
+                       throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end'));
                }
                // Username only consists of 1 or 2 dots (directory traversal)
                if ($uid === '.' || $uid === '..') {
-                       throw new \Exception($l->t('Username must not consist of dots only'));
+                       throw new \InvalidArgumentException($l->t('Username must not consist of dots only'));
                }
                // No empty password
-               if (trim($password) == '') {
-                       throw new \Exception($l->t('A valid password must be provided'));
+               if (trim($password) === '') {
+                       throw new \InvalidArgumentException($l->t('A valid password must be provided'));
                }
 
                // Check if user already exists
                if ($this->userExists($uid)) {
-                       throw new \Exception($l->t('The username is already being used'));
+                       throw new \InvalidArgumentException($l->t('The username is already being used'));
                }
 
-               $this->emit('\OC\User', 'preCreateUser', array($uid, $password));
-               foreach ($this->backends as $backend) {
-                       if ($backend->implementsActions(Backend::CREATE_USER)) {
-                               $backend->createUser($uid, $password);
-                               $user = $this->getUserObject($uid, $backend);
-                               $this->emit('\OC\User', 'postCreateUser', array($user, $password));
-                               return $user;
-                       }
-               }
-
-               return false;
-       }
-
-       /**
-        * @param string $uid
-        * @param string $password
-        * @param UserInterface $backend
-        * @return IUser|null
-        */
-       public function createUserFromBackend($uid, $password, UserInterface $backend) {
                $this->emit('\OC\User', 'preCreateUser', [$uid, $password]);
                $backend->createUser($uid, $password);
                $user = $this->getUserObject($uid, $backend);
index 2287a24e9ec30aff27caafea7ead57869b1a51fc..1ec392dfd826497204c3e7176d6c28bdb7491eaf 100644 (file)
@@ -123,7 +123,7 @@ interface IUserManager {
        /**
         * @param string $uid
         * @param string $password
-        * @throws \Exception
+        * @throws \InvalidArgumentException
         * @return bool|\OCP\IUser the created user of false
         * @since 8.0.0
         */
@@ -134,6 +134,7 @@ interface IUserManager {
         * @param string $password
         * @param UserInterface $backend
         * @return IUser|null
+        * @throws \InvalidArgumentException
         * @since 12.0.0
         */
        public function createUserFromBackend($uid, $password, UserInterface $backend);
index 671b2ac57c1d5a9f2ea51ea711924157e0878ade..aaee64f18638223f091a4c9ad3381c5a7c5549e1 100644 (file)
@@ -291,10 +291,18 @@ class ManagerTest extends TestCase {
         * @dataProvider dataCreateUserInvalid
         */
        public function testCreateUserInvalid($uid, $password, $exception) {
+               /** @var \Test\Util\User\Dummy|\PHPUnit_Framework_MockObject_MockObject $backend */
+               $backend = $this->createMock(\Test\Util\User\Dummy::class);
+               $backend->expects($this->once())
+                       ->method('implementsActions')
+                       ->with(\OC\User\Backend::CREATE_USER)
+                       ->willReturn(true);
 
-               $this->setExpectedException(\Exception::class, $exception);
 
                $manager = new \OC\User\Manager($this->config);
+               $manager->registerBackend($backend);
+
+               $this->setExpectedException(\InvalidArgumentException::class, $exception);
                $manager->createUser($uid, $password);
 
        }
@@ -362,10 +370,8 @@ class ManagerTest extends TestCase {
                $backend->expects($this->never())
                        ->method('createUser');
 
-               $backend->expects($this->once())
-                       ->method('userExists')
-                       ->with($this->equalTo('foo'))
-                       ->will($this->returnValue(false));
+               $backend->expects($this->never())
+                       ->method('userExists');
 
                $manager = new \OC\User\Manager($this->config);
                $manager->registerBackend($backend);