add invites!
This commit is contained in:
parent
a8a1d48f10
commit
c53e790fb7
7 changed files with 130 additions and 17 deletions
|
@ -1,15 +0,0 @@
|
||||||
#!/usr/bin/env php
|
|
||||||
<?php
|
|
||||||
|
|
||||||
$basedir = realpath(__DIR__ . '/../');
|
|
||||||
# All calls are now relative to the root directory
|
|
||||||
chdir($basedir);
|
|
||||||
|
|
||||||
include $basedir . '/vendor/autoload.php';
|
|
||||||
|
|
||||||
$core = new \Eater\Glim\Core();
|
|
||||||
$core->startTimer(["total"]);
|
|
||||||
$core->boot($basedir);
|
|
||||||
echo $core->get('user')->createInvite();
|
|
||||||
$core->endTimer(["total"]);
|
|
||||||
|
|
|
@ -32,6 +32,10 @@ routes:
|
||||||
post: Panel\Servers\Edit\Action
|
post: Panel\Servers\Edit\Action
|
||||||
/{fingerprint}/config: Panel\Servers\Config
|
/{fingerprint}/config: Panel\Servers\Config
|
||||||
/{fingerprint}/config/{cert}: Panel\Servers\Config
|
/{fingerprint}/config/{cert}: Panel\Servers\Config
|
||||||
|
/invites:
|
||||||
|
get: Panel\Invites
|
||||||
|
/create:
|
||||||
|
post: Panel\Invites\Create
|
||||||
/server:
|
/server:
|
||||||
/register:
|
/register:
|
||||||
post: Server\Register
|
post: Server\Register
|
29
src/Handler/Panel/Invites.php
Normal file
29
src/Handler/Panel/Invites.php
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Eater\Glim\Handler\Panel;
|
||||||
|
|
||||||
|
|
||||||
|
use Aura\Session\Segment;
|
||||||
|
use Eater\Glim\Handler\Session;
|
||||||
|
|
||||||
|
class Invites extends Session
|
||||||
|
{
|
||||||
|
protected $shouldHaveUser = true;
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$user = $this->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()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
29
src/Handler/Panel/Invites/Create.php
Normal file
29
src/Handler/Panel/Invites/Create.php
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Eater\Glim\Handler\Panel\Invites;
|
||||||
|
|
||||||
|
|
||||||
|
use Eater\Glim\Handler\Session;
|
||||||
|
use Eater\Glim\Service\User;
|
||||||
|
use Aura\Session\Segment;
|
||||||
|
|
||||||
|
class Create extends Session
|
||||||
|
{
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
/** @var User $user */
|
||||||
|
$user = $this->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');
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,7 +13,7 @@ class User extends Main
|
||||||
* @param string $invite
|
* @param string $invite
|
||||||
* @param string $username
|
* @param string $username
|
||||||
* @param string $password
|
* @param string $password
|
||||||
* @return Eater\Glim\Model\User
|
* @return UserModel
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function register($invite, $username, $password)
|
public function register($invite, $username, $password)
|
||||||
|
@ -26,9 +26,20 @@ class User extends Main
|
||||||
|
|
||||||
$this->validateUserParams($username, $password);
|
$this->validateUserParams($username, $password);
|
||||||
|
|
||||||
|
$inviteUser = $invite->getUser();
|
||||||
|
|
||||||
$user = new UserModel();
|
$user = new UserModel();
|
||||||
$user->setUsername($username);
|
$user->setUsername($username);
|
||||||
$user->setPassword(\password_hash($password, PASSWORD_DEFAULT));
|
$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();
|
$user->save();
|
||||||
|
|
||||||
$invite->delete();
|
$invite->delete();
|
||||||
|
@ -66,6 +77,7 @@ class User extends Main
|
||||||
$user->setUsername($username);
|
$user->setUsername($username);
|
||||||
$user->setPassword(\password_hash($password, PASSWORD_DEFAULT));
|
$user->setPassword(\password_hash($password, PASSWORD_DEFAULT));
|
||||||
$user->setSuperuser(true);
|
$user->setSuperuser(true);
|
||||||
|
$user->setMaxInvites(-1);
|
||||||
$user->save();
|
$user->save();
|
||||||
|
|
||||||
return $user;
|
return $user;
|
||||||
|
@ -94,14 +106,19 @@ class User extends Main
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param UserModel $user
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function createInvite()
|
public function createInvite(UserModel $user)
|
||||||
{
|
{
|
||||||
$invite = new Invite();
|
$invite = new Invite();
|
||||||
$invite->setInvite(bin2hex(openssl_random_pseudo_bytes(20)));
|
$invite->setInvite(bin2hex(openssl_random_pseudo_bytes(20)));
|
||||||
|
$invite->setUser($user);
|
||||||
$invite->save();
|
$invite->save();
|
||||||
|
|
||||||
|
$user->setUsedInvites($user->getUsedInvites() + 1);
|
||||||
|
$user->save();
|
||||||
|
|
||||||
return $invite->getInvite();
|
return $invite->getInvite();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
<div class="pull-right">
|
<div class="pull-right">
|
||||||
{% if user %}
|
{% if user %}
|
||||||
<a href="/panel" class="user navbar-brand">{{ user.username }}</a>
|
<a href="/panel" class="user navbar-brand">{{ user.username }}</a>
|
||||||
|
<a href="/panel/invites" class="navbar-brand">Invites</a>
|
||||||
<a href="/logout" class="navbar-brand">Logout</a>
|
<a href="/logout" class="navbar-brand">Logout</a>
|
||||||
{% else %}
|
{% else %}
|
||||||
<a class="navbar-brand" href="/login">Login</a>
|
<a class="navbar-brand" href="/login">Login</a>
|
||||||
|
|
48
views/panel/invites.html.twig
Normal file
48
views/panel/invites.html.twig
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
{% extends "base_bootstrap.html.twig" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<h2 id="certificates">Invites <small>You used {{ used_invites }} from your {{ max_invites == -1 ? 'infinite' : max_invites }} invites</small></h2>
|
||||||
|
</div>
|
||||||
|
{% if error %}
|
||||||
|
<div class="row">
|
||||||
|
<div class="alert alert-warning" role="alert">
|
||||||
|
{{ error }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
<div class="row">
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Invite</th>
|
||||||
|
<th>
|
||||||
|
{% if max_invites > used_invites or max_invites == -1 %}
|
||||||
|
<form action="/panel/invites/create" method="post">
|
||||||
|
<button class="btn btn-default pull-right" type="submit">Create new invite</button>
|
||||||
|
</form>
|
||||||
|
{% endif %}
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for invite in invites %}
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<kbd>{{ invite.getInvite() }}</kbd>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<button class="btn btn-default">Copy</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% else %}
|
||||||
|
<tr>
|
||||||
|
<td colspan="2">You don't have any invites :(</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
Loading…
Reference in a new issue