/** * 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]); }
/** * Count all the users * * @return int */ private function countUsers() { $allCount = $this->userManager->countUsers(); $totalCount = 0; foreach ($allCount as $backend => $count) { $totalCount += $count; } return $totalCount; }
private function countUsers() { return $this->userManager->countUsers(); }
/** * @NoAdminRequired * * @param string $username * @param string $password * @param array $groups * @param string $email * @return DataResponse */ public function create($username, $password, array $groups = array(), $email = '') { $userCountArray = $this->userManager->countUsers(); $userCount = 0; if (!empty($userCountArray)) { foreach ($userCountArray as $classname => $usercount) { $userCount += $usercount; } } if ($userCount > 3) { return new DataResponse(array('message' => (string) $this->l10n->t('User number exceeds the license!')), Http::STATUS_UNPROCESSABLE_ENTITY); } if ($email !== '' && !$this->mailer->validateMailAddress($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 { $message = $this->mailer->createMessage(); $message->setTo([$email => $username]); $message->setSubject($subject); $message->setHtmlBody($mailContent); $message->setPlainBody($plainTextMailContent); $message->setFrom([$this->fromMailAddress => $this->defaults->getName()]); $this->mailer->send($message); } 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); }