/**
  * @NoAdminRequired
  *
  * @param string $username
  * @param string $password
  * @param array $groups
  * @param string $email
  * @return DataResponse
  */
 public function create($username, $password, array $groups = array(), $email = '')
 {
     if ($email !== '' && !$this->mail->validateAddress($email)) {
         return new DataResponse(array('message' => (string) $this->l10n->t('Invalid mail address')), Http::STATUS_UNPROCESSABLE_ENTITY);
     }
     if (!$this->isAdmin) {
         $userId = $this->userSession->getUser()->getUID();
         if (!empty($groups)) {
             foreach ($groups as $key => $group) {
                 if (!$this->subAdminFactory->isGroupAccessible($userId, $group)) {
                     unset($groups[$key]);
                 }
             }
         }
         if (empty($groups)) {
             $groups = $this->subAdminFactory->getSubAdminsOfGroups($userId);
         }
     }
     if ($this->userManager->userExists($username)) {
         return new DataResponse(array('message' => (string) $this->l10n->t('A user with that name already exists.')), Http::STATUS_CONFLICT);
     }
     try {
         $user = $this->userManager->createUser($username, $password);
     } catch (\Exception $exception) {
         return new DataResponse(array('message' => (string) $this->l10n->t('Unable to create user.')), Http::STATUS_FORBIDDEN);
     }
     if ($user instanceof User) {
         if ($groups !== null) {
             foreach ($groups as $groupName) {
                 $group = $this->groupManager->get($groupName);
                 if (empty($group)) {
                     $group = $this->groupManager->createGroup($groupName);
                 }
                 $group->addUser($user);
             }
         }
         /**
          * Send new user mail only if a mail is set
          */
         if ($email !== '') {
             $this->config->setUserValue($username, 'settings', 'email', $email);
             // data for the mail template
             $mailData = array('username' => $username, 'url' => $this->urlGenerator->getAbsoluteURL('/'));
             $mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank');
             $mailContent = $mail->render();
             $mail = new TemplateResponse('settings', 'email.new_user_plain_text', $mailData, 'blank');
             $plainTextMailContent = $mail->render();
             $subject = $this->l10n->t('Your %s account was created', [$this->defaults->getName()]);
             try {
                 $this->mail->send($email, $username, $subject, $mailContent, $this->fromMailAddress, $this->defaults->getName(), 1, $plainTextMailContent);
             } catch (\Exception $e) {
                 $this->log->error("Can't send new user mail to {$email}: " . $e->getMessage(), array('app' => 'settings'));
             }
         }
         // fetch users groups
         $userGroups = $this->groupManager->getUserGroupIds($user);
         return new DataResponse($this->formatUserForIndex($user, $userGroups), Http::STATUS_CREATED);
     }
     return new DataResponse(array('message' => (string) $this->l10n->t('Unable to create user.')), Http::STATUS_FORBIDDEN);
 }
 /**
  * Count all unique users visible for the current admin/subadmin.
  *
  * @NoAdminRequired
  *
  * @return DataResponse
  */
 public function stats()
 {
     $userCount = 0;
     if ($this->isAdmin) {
         $countByBackend = $this->userManager->countUsers();
         if (!empty($countByBackend)) {
             foreach ($countByBackend as $count) {
                 $userCount += $count;
             }
         }
     } else {
         $groupNames = $this->subAdminFactory->getSubAdminsOfGroups($this->userSession->getUser()->getUID());
         $uniqueUsers = [];
         foreach ($groupNames as $groupName) {
             $group = $this->groupManager->get($groupName);
             if (!is_null($group)) {
                 foreach ($group->getUsers() as $uid => $displayName) {
                     $uniqueUsers[$uid] = true;
                 }
             }
         }
         $userCount = count($uniqueUsers);
     }
     return new DataResponse(['totalUsers' => $userCount]);
 }