diff --git a/bin/create-invite.php b/bin/create-invite.php deleted file mode 100644 index eaa503a..0000000 --- a/bin/create-invite.php +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env php -startTimer(["total"]); -$core->boot($basedir); -echo $core->get('user')->createInvite(); -$core->endTimer(["total"]); - diff --git a/config/routes.yml b/config/routes.yml index 8338904..c521124 100644 --- a/config/routes.yml +++ b/config/routes.yml @@ -32,6 +32,10 @@ routes: post: Panel\Servers\Edit\Action /{fingerprint}/config: Panel\Servers\Config /{fingerprint}/config/{cert}: Panel\Servers\Config + /invites: + get: Panel\Invites + /create: + post: Panel\Invites\Create /server: /register: post: Server\Register \ No newline at end of file diff --git a/src/Handler/Panel/Invites.php b/src/Handler/Panel/Invites.php new file mode 100644 index 0000000..486f6e3 --- /dev/null +++ b/src/Handler/Panel/Invites.php @@ -0,0 +1,29 @@ +getUser(); + + $invites = $user->getInvites(); + + /** @var Segment $segment */ + $segment = $this->get('session')->getSegment('main'); + + return $this->render('panel/invites.html.twig', [ + 'invites' => $invites, + 'error' => $segment->getFlash('error'), + 'used_invites' => $user->getUsedInvites(), + 'max_invites' => $user->getMaxInvites() + ]); + } +} \ No newline at end of file diff --git a/src/Handler/Panel/Invites/Create.php b/src/Handler/Panel/Invites/Create.php new file mode 100644 index 0000000..56ddef4 --- /dev/null +++ b/src/Handler/Panel/Invites/Create.php @@ -0,0 +1,29 @@ +get('user'); + + $userModel = $this->getUser(); + + if ($userModel->getUsedInvites() < $userModel->getMaxInvites() || $userModel->getMaxInvites() === -1) { + $user->createInvite($userModel); + } else { + /** @var Segment $segment */ + $segment = $this->get('session')->getSegment('main'); + $segment->setFlash('error', 'You dont have any invites anymore'); + } + + return $this->redirect('/panel/invites'); + } +} \ No newline at end of file diff --git a/src/Service/User.php b/src/Service/User.php index 6329ac2..c30d697 100644 --- a/src/Service/User.php +++ b/src/Service/User.php @@ -13,7 +13,7 @@ class User extends Main * @param string $invite * @param string $username * @param string $password - * @return Eater\Glim\Model\User + * @return UserModel * @throws \Exception */ public function register($invite, $username, $password) @@ -26,9 +26,20 @@ class User extends Main $this->validateUserParams($username, $password); + $inviteUser = $invite->getUser(); + $user = new UserModel(); $user->setUsername($username); $user->setPassword(\password_hash($password, PASSWORD_DEFAULT)); + + if ($inviteUser === null || $inviteUser->getMaxInvites() === -1) { + $user->setMaxInvites(5); + } elseif ($inviteUser->getMaxInvites() > 0) { + $user->setMaxInvites($inviteUser->getMaxInvites() - 1); + } else { + $user->setMaxInvites(0); + } + $user->save(); $invite->delete(); @@ -66,6 +77,7 @@ class User extends Main $user->setUsername($username); $user->setPassword(\password_hash($password, PASSWORD_DEFAULT)); $user->setSuperuser(true); + $user->setMaxInvites(-1); $user->save(); return $user; @@ -94,14 +106,19 @@ class User extends Main } /** + * @param UserModel $user * @return string */ - public function createInvite() + public function createInvite(UserModel $user) { $invite = new Invite(); $invite->setInvite(bin2hex(openssl_random_pseudo_bytes(20))); + $invite->setUser($user); $invite->save(); + $user->setUsedInvites($user->getUsedInvites() + 1); + $user->save(); + return $invite->getInvite(); } } diff --git a/views/base_bootstrap.html.twig b/views/base_bootstrap.html.twig index 9279db6..e3d2e5a 100644 --- a/views/base_bootstrap.html.twig +++ b/views/base_bootstrap.html.twig @@ -20,6 +20,7 @@
Invite | ++ {% if max_invites > used_invites or max_invites == -1 %} + + {% endif %} + | +
---|---|
+ {{ invite.getInvite() }} + | ++ + | +
You don't have any invites :( | +