add invites!

master
Corne Oppelaar 9 years ago
parent a8a1d48f10
commit c53e790fb7

@ -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
/{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

@ -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()
]);
}
}

@ -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 $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();
}
}

@ -20,6 +20,7 @@
<div class="pull-right">
{% if user %}
<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>
{% else %}
<a class="navbar-brand" href="/login">Login</a>

@ -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…
Cancel
Save