owncloud/core

View on GitHub
settings/Controller/UsersController.php

Summary

Maintainability
F
1 wk
Test Coverage

File UsersController.php has 974 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
 * @author Clark Tomlinson <fallen013@gmail.com>
 * @author Joas Schilling <coding@schilljs.com>
Severity: Major
Found in settings/Controller/UsersController.php - About 2 days to fix

    Function create has a Cognitive Complexity of 46 (exceeds 5 allowed). Consider refactoring.
    Open

        public function create($username, $password, array $groups= [], $email='') {
            $resp = $this->validateString($username, 255);
            if ($resp) {
                return $resp;
            }
    Severity: Minor
    Found in settings/Controller/UsersController.php - About 7 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function index has a Cognitive Complexity of 39 (exceeds 5 allowed). Consider refactoring.
    Open

        public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
            // FIXME: The JS sends the group '_everyone' instead of no GID for the "all users" group.
            if ($gid === '_everyone') {
                $gid = '';
            }
    Severity: Minor
    Found in settings/Controller/UsersController.php - About 5 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Method create has 96 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        public function create($username, $password, array $groups= [], $email='') {
            $resp = $this->validateString($username, 255);
            if ($resp) {
                return $resp;
            }
    Severity: Major
    Found in settings/Controller/UsersController.php - About 3 hrs to fix

      Method setMailAddress has 92 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          public function setMailAddress($id, $mailAddress) {
              $userId = $this->userSession->getUser()->getUID();
              $user = $this->userManager->get($id);
      
              if ($userId !== $id
      Severity: Major
      Found in settings/Controller/UsersController.php - About 3 hrs to fix

        UsersController has 24 functions (exceeds 20 allowed). Consider refactoring.
        Open

        class UsersController extends Controller {
            /** @var IL10N */
            private $l10n;
            /** @var Session */
            private $userSession;
        Severity: Minor
        Found in settings/Controller/UsersController.php - About 2 hrs to fix

          Method setPassword has 64 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              public function setPassword($token, $userId, $password) {
                  $user = $this->userManager->get($userId);
          
                  if ($user === null) {
                      $this->log->error('User: ' . $userId . ' does not exist.', ['app' => 'settings']);
          Severity: Major
          Found in settings/Controller/UsersController.php - About 2 hrs to fix

            Method index has 62 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
                    // FIXME: The JS sends the group '_everyone' instead of no GID for the "all users" group.
                    if ($gid === '_everyone') {
                        $gid = '';
                    }
            Severity: Major
            Found in settings/Controller/UsersController.php - About 2 hrs to fix

              Method __construct has 18 arguments (exceeds 4 allowed). Consider refactoring.
              Open

                      $appName,
                      IRequest $request,
                      IUserManager $userManager,
                      IGroupManager $groupManager,
                      Session $userSession,
              Severity: Major
              Found in settings/Controller/UsersController.php - About 2 hrs to fix

                Method formatUserForIndex has 54 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                    private function formatUserForIndex(IUser $user, array $userGroups = null) {
                        // TODO: eliminate this encryption specific code below and somehow
                        // hook in additional user info from other apps
                
                        // recovery isn't possible if admin or user has it disabled and encryption
                Severity: Major
                Found in settings/Controller/UsersController.php - About 2 hrs to fix

                  Method setEnabled has 50 lines of code (exceeds 25 allowed). Consider refactoring.
                  Open

                      public function setEnabled($id, $enabled) {
                          $userId = $this->userSession->getUser()->getUID();
                          $user = $this->userManager->get($id);
                          '@phan-var \OC\Group\Manager $this->groupManager';
                  
                  
                  Severity: Minor
                  Found in settings/Controller/UsersController.php - About 2 hrs to fix

                    Method resendInvitation has 48 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                        public function resendInvitation($userId) {
                            $user = $this->userManager->get($userId);
                            $currentUser = $this->userSession->getUser();
                    
                            if ($user === null || $this->userTypeHelper->isGuestUser($userId) === true) {
                    Severity: Minor
                    Found in settings/Controller/UsersController.php - About 1 hr to fix

                      Method destroy has 47 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                          public function destroy($id) {
                              $userId = $this->userSession->getUser()->getUID();
                              $user = $this->userManager->get($id);
                      
                              if ($userId === $id) {
                      Severity: Minor
                      Found in settings/Controller/UsersController.php - About 1 hr to fix

                        Method setDisplayName has 43 lines of code (exceeds 25 allowed). Consider refactoring.
                        Open

                            public function setDisplayName($username, $displayName) {
                                $resp = $this->validateString($displayName, 64);
                                if ($resp) {
                                    return $resp;
                                }
                        Severity: Minor
                        Found in settings/Controller/UsersController.php - About 1 hr to fix

                          Method resendToken has 43 lines of code (exceeds 25 allowed). Consider refactoring.
                          Open

                              public function resendToken($userId) {
                                  $user = $this->userManager->get($userId);
                          
                                  if ($user === null) {
                                      $this->log->error('User: ' . $userId . ' does not exist', ['app' => 'settings']);
                          Severity: Minor
                          Found in settings/Controller/UsersController.php - About 1 hr to fix

                            Function stats has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
                            Open

                                public function stats() {
                                    $userCount = 0;
                                    if ($this->isAdmin) {
                                        $countByBackend = $this->userManager->countUsers();
                            
                            
                            Severity: Minor
                            Found in settings/Controller/UsersController.php - About 1 hr to fix

                            Cognitive Complexity

                            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                            A method's cognitive complexity is based on a few simple rules:

                            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                            • Code is considered more complex for each "break in the linear flow of the code"
                            • Code is considered more complex when "flow breaking structures are nested"

                            Further reading

                            Function setMailAddress has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
                            Open

                                public function setMailAddress($id, $mailAddress) {
                                    $userId = $this->userSession->getUser()->getUID();
                                    $user = $this->userManager->get($id);
                            
                                    if ($userId !== $id
                            Severity: Minor
                            Found in settings/Controller/UsersController.php - About 1 hr to fix

                            Cognitive Complexity

                            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                            A method's cognitive complexity is based on a few simple rules:

                            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                            • Code is considered more complex for each "break in the linear flow of the code"
                            • Code is considered more complex when "flow breaking structures are nested"

                            Further reading

                            Method changeMail has 34 lines of code (exceeds 25 allowed). Consider refactoring.
                            Open

                                public function changeMail($token, $userId) {
                                    $user = $this->userManager->get($userId);
                                    $sessionUser = $this->userSession->getUser();
                            
                                    if ($user->getUID() !== $sessionUser->getUID()) {
                            Severity: Minor
                            Found in settings/Controller/UsersController.php - About 1 hr to fix

                              Method sendEmail has 32 lines of code (exceeds 25 allowed). Consider refactoring.
                              Open

                                  public function sendEmail($userId, $mailAddress) {
                                      $token = $this->config->getUserValue($userId, 'owncloud', 'changeMail');
                                      if ($token !== '') {
                                          $splittedToken = \explode(':', $token);
                                          if ((\count($splittedToken)) === 3 && $splittedToken[0] > ($this->timeFactory->getTime() - 60 * 5)) {
                              Severity: Minor
                              Found in settings/Controller/UsersController.php - About 1 hr to fix

                                Method setPasswordForm has 31 lines of code (exceeds 25 allowed). Consider refactoring.
                                Open

                                    public function setPasswordForm($token, $userId) {
                                        try {
                                            $this->checkPasswordSetToken($token, $userId);
                                        } catch (UserTokenException $e) {
                                            if ($e instanceof UserTokenExpiredException) {
                                Severity: Minor
                                Found in settings/Controller/UsersController.php - About 1 hr to fix

                                  Method generateTokenAndSendMail has 28 lines of code (exceeds 25 allowed). Consider refactoring.
                                  Open

                                      private function generateTokenAndSendMail($userId, $email) {
                                          $token = $this->secureRandom->generate(
                                              21,
                                              ISecureRandom::CHAR_DIGITS,
                                              ISecureRandom::CHAR_LOWER,
                                  Severity: Minor
                                  Found in settings/Controller/UsersController.php - About 1 hr to fix

                                    Function formatUserForIndex has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                                    Open

                                        private function formatUserForIndex(IUser $user, array $userGroups = null) {
                                            // TODO: eliminate this encryption specific code below and somehow
                                            // hook in additional user info from other apps
                                    
                                            // recovery isn't possible if admin or user has it disabled and encryption
                                    Severity: Minor
                                    Found in settings/Controller/UsersController.php - About 1 hr to fix

                                    Cognitive Complexity

                                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                    A method's cognitive complexity is based on a few simple rules:

                                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                    • Code is considered more complex for each "break in the linear flow of the code"
                                    • Code is considered more complex when "flow breaking structures are nested"

                                    Further reading

                                    Function setEmailAddress has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                                    Open

                                        public function setEmailAddress($id, $mailAddress) {
                                            $resp = $this->validateEMail($mailAddress);
                                            if ($resp) {
                                                return $resp;
                                            }
                                    Severity: Minor
                                    Found in settings/Controller/UsersController.php - About 45 mins to fix

                                    Cognitive Complexity

                                    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                    A method's cognitive complexity is based on a few simple rules:

                                    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                    • Code is considered more complex for each "break in the linear flow of the code"
                                    • Code is considered more complex when "flow breaking structures are nested"

                                    Further reading

                                    Method index has 5 arguments (exceeds 4 allowed). Consider refactoring.
                                    Open

                                        public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
                                    Severity: Minor
                                    Found in settings/Controller/UsersController.php - About 35 mins to fix

                                      Function setDisplayName has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                                      Open

                                          public function setDisplayName($username, $displayName) {
                                              $resp = $this->validateString($displayName, 64);
                                              if ($resp) {
                                                  return $resp;
                                              }
                                      Severity: Minor
                                      Found in settings/Controller/UsersController.php - About 35 mins to fix

                                      Cognitive Complexity

                                      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                      A method's cognitive complexity is based on a few simple rules:

                                      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                      • Code is considered more complex for each "break in the linear flow of the code"
                                      • Code is considered more complex when "flow breaking structures are nested"

                                      Further reading

                                      Function resendInvitation has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                                      Open

                                          public function resendInvitation($userId) {
                                              $user = $this->userManager->get($userId);
                                              $currentUser = $this->userSession->getUser();
                                      
                                              if ($user === null || $this->userTypeHelper->isGuestUser($userId) === true) {
                                      Severity: Minor
                                      Found in settings/Controller/UsersController.php - About 35 mins to fix

                                      Cognitive Complexity

                                      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                      A method's cognitive complexity is based on a few simple rules:

                                      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                      • Code is considered more complex for each "break in the linear flow of the code"
                                      • Code is considered more complex when "flow breaking structures are nested"

                                      Further reading

                                      Avoid too many return statements within this method.
                                      Open

                                                  return new JSONResponse(
                                                      ["error" => $this->l10n->t('Can\'t send email to the user. Contact your administrator.')],
                                                      HTTP::STATUS_INTERNAL_SERVER_ERROR
                                                  );
                                      Severity: Major
                                      Found in settings/Controller/UsersController.php - About 30 mins to fix

                                        Avoid too many return statements within this method.
                                        Open

                                                    return new JSONResponse(
                                                        [
                                                            'status' => 'error',
                                                            'message' => $this->l10n->t('Failed to send email. Please contact your administrator.'),
                                                            'type' => 'emailsendfailed'
                                        Severity: Major
                                        Found in settings/Controller/UsersController.php - About 30 mins to fix

                                          Avoid too many return statements within this method.
                                          Open

                                                  return new JSONResponse(['status' => 'success']);
                                          Severity: Major
                                          Found in settings/Controller/UsersController.php - About 30 mins to fix

                                            Avoid too many return statements within this method.
                                            Open

                                                        return new DataResponse(
                                                            [
                                                                'status' => 'error',
                                                                'data' => [
                                                                    'message' => (string)$e->getMessage()
                                            Severity: Major
                                            Found in settings/Controller/UsersController.php - About 30 mins to fix

                                              Avoid too many return statements within this method.
                                              Open

                                                              return new DataResponse(
                                                                  [
                                                                      'status' => 'success',
                                                                      'data' => [
                                                                          'username' => $id,
                                              Severity: Major
                                              Found in settings/Controller/UsersController.php - About 30 mins to fix

                                                Avoid too many return statements within this method.
                                                Open

                                                        return new DataResponse(
                                                            [
                                                                'message' => (string)$this->l10n->t('Unable to create user.')
                                                            ],
                                                            Http::STATUS_FORBIDDEN
                                                Severity: Major
                                                Found in settings/Controller/UsersController.php - About 30 mins to fix

                                                  Avoid too many return statements within this method.
                                                  Open

                                                                  return new DataResponse(
                                                                      [
                                                                          'status' => 'error',
                                                                          'data' => [
                                                                              'username' => $id,
                                                  Severity: Major
                                                  Found in settings/Controller/UsersController.php - About 30 mins to fix

                                                    Avoid too many return statements within this method.
                                                    Open

                                                            return new JSONResponse();
                                                    Severity: Major
                                                    Found in settings/Controller/UsersController.php - About 30 mins to fix

                                                      Avoid too many return statements within this method.
                                                      Open

                                                                  return new DataResponse(
                                                                      $this->formatUserForIndex($user, $userGroups),
                                                                      Http::STATUS_CREATED
                                                                  );
                                                      Severity: Major
                                                      Found in settings/Controller/UsersController.php - About 30 mins to fix

                                                        Avoid too many return statements within this method.
                                                        Open

                                                                    return new DataResponse(
                                                                        [
                                                                            'status' => 'success',
                                                                            'data' => [
                                                                                'message' => (string)$this->l10n->t('Email has been changed successfully.')
                                                        Severity: Major
                                                        Found in settings/Controller/UsersController.php - About 30 mins to fix

                                                          Function setEnabled has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                                          Open

                                                              public function setEnabled($id, $enabled) {
                                                                  $userId = $this->userSession->getUser()->getUID();
                                                                  $user = $this->userManager->get($id);
                                                                  '@phan-var \OC\Group\Manager $this->groupManager';
                                                          
                                                          
                                                          Severity: Minor
                                                          Found in settings/Controller/UsersController.php - About 25 mins to fix

                                                          Cognitive Complexity

                                                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                                          A method's cognitive complexity is based on a few simple rules:

                                                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                                          • Code is considered more complex for each "break in the linear flow of the code"
                                                          • Code is considered more complex when "flow breaking structures are nested"

                                                          Further reading

                                                          Function changeMail has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                                          Open

                                                              public function changeMail($token, $userId) {
                                                                  $user = $this->userManager->get($userId);
                                                                  $sessionUser = $this->userSession->getUser();
                                                          
                                                                  if ($user->getUID() !== $sessionUser->getUID()) {
                                                          Severity: Minor
                                                          Found in settings/Controller/UsersController.php - About 25 mins to fix

                                                          Cognitive Complexity

                                                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                                          A method's cognitive complexity is based on a few simple rules:

                                                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                                          • Code is considered more complex for each "break in the linear flow of the code"
                                                          • Code is considered more complex when "flow breaking structures are nested"

                                                          Further reading

                                                          Function destroy has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                                                          Open

                                                              public function destroy($id) {
                                                                  $userId = $this->userSession->getUser()->getUID();
                                                                  $user = $this->userManager->get($id);
                                                          
                                                                  if ($userId === $id) {
                                                          Severity: Minor
                                                          Found in settings/Controller/UsersController.php - About 25 mins to fix

                                                          Cognitive Complexity

                                                          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                                                          A method's cognitive complexity is based on a few simple rules:

                                                          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                                                          • Code is considered more complex for each "break in the linear flow of the code"
                                                          • Code is considered more complex when "flow breaking structures are nested"

                                                          Further reading

                                                          Similar blocks of code found in 2 locations. Consider refactoring.
                                                          Open

                                                                      try {
                                                                          $message = $this->mailer->createMessage();
                                                                          $message->setTo([$oldEmailAddress => $userId]);
                                                                          $message->setSubject($this->l10n->t('%s email address changed successfully', [$this->defaults->getName()]));
                                                                          $message->setPlainBody($msg);
                                                          Severity: Major
                                                          Found in settings/Controller/UsersController.php and 1 other location - About 2 hrs to fix
                                                          settings/Controller/UsersController.php on lines 1154..1165

                                                          Duplicated Code

                                                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                                          Tuning

                                                          This issue has a mass of 126.

                                                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                                          Refactorings

                                                          Further Reading

                                                          Similar blocks of code found in 2 locations. Consider refactoring.
                                                          Open

                                                                  try {
                                                                      $message = $this->mailer->createMessage();
                                                                      $message->setTo([$mailAddress => $userId]);
                                                                      $message->setSubject($this->l10n->t('%s email address confirm', [$this->defaults->getName()]));
                                                                      $message->setPlainBody($msg);
                                                          Severity: Major
                                                          Found in settings/Controller/UsersController.php and 1 other location - About 2 hrs to fix
                                                          settings/Controller/UsersController.php on lines 1238..1249

                                                          Duplicated Code

                                                          Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                                                          Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                                                          When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                                                          Tuning

                                                          This issue has a mass of 126.

                                                          We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                                                          The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                                                          If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                                                          See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                                                          Refactorings

                                                          Further Reading

                                                          There are no issues that match your filters.

                                                          Category
                                                          Status