Add user invites

This commit is contained in:
Daniel Supernault 2019-02-15 20:13:24 -07:00
parent d127fbf290
commit da0b3b5f1c
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 106 additions and 0 deletions

View file

@ -0,0 +1,52 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App\UserInvite;
use Illuminate\Support\Str;
class UserInviteController extends Controller
{
public function __construct()
{
abort_if(!config('pixelfed.user_invites.enabled'), 404);
}
public function create(Request $request)
{
abort_unless(Auth::check(), 403);
return view('settings.invites.create');
}
public function show(Request $request)
{
abort_unless(Auth::check(), 403);
$invites = UserInvite::whereUserId(Auth::id())->paginate(10);
$limit = config('pixelfed.user_invites.limit.total');
$used = UserInvite::whereUserId(Auth::id())->count();
return view('settings.invites.home', compact('invites', 'limit', 'used'));
}
public function store(Request $request)
{
abort_unless(Auth::check(), 403);
$this->validate($request, [
'email' => 'required|email|unique:users|unique:user_invites',
'message' => 'nullable|string|max:500',
'tos' => 'required|accepted'
]);
$invite = new UserInvite;
$invite->user_id = Auth::id();
$invite->profile_id = Auth::user()->profile->id;
$invite->email = $request->input('email');
$invite->message = $request->input('message');
$invite->key = (string) Str::uuid();
$invite->token = str_random(32);
$invite->save();
return redirect(route('settings.invites'));
}
}

15
app/UserInvite.php Normal file
View file

@ -0,0 +1,15 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserInvite extends Model
{
public function url()
{
$path = '/i/invite/code';
$url = url($path, [$this->key, $this->token]);
return $url;
}
}

View file

@ -0,0 +1,39 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUserInvitesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_invites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned()->index();
$table->bigInteger('profile_id')->unsigned()->index();
$table->string('email')->unique()->index();
$table->text('message')->nullable();
$table->string('key');
$table->string('token');
$table->timestamp('valid_until')->nullable();
$table->timestamp('used_at')->nullable()->index();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_invites');
}
}