Skip to content

Commit

Permalink
Merge pull request #123 from ControlPanel-gg/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
AVMG20 authored Jul 12, 2021
2 parents 40ee71d + a486307 commit af508de
Show file tree
Hide file tree
Showing 24 changed files with 1,753 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function update(Request $request, User $user)
"name" => "required|string|min:4|max:30",
"pterodactyl_id" => "required|numeric|unique:users,pterodactyl_id,{$user->id}",
"email" => "required|string|email",
"credits" => "required|numeric|min:0|max:999999",
"credits" => "required|numeric|min:0|max:99999999",
"server_limit" => "required|numeric|min:0|max:1000000",
"role" => Rule::in(['admin', 'mod', 'client', 'member']),
]);
Expand Down
196 changes: 196 additions & 0 deletions app/Http/Controllers/Admin/VoucherController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Voucher;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;

class VoucherController extends Controller
{
/**
* Display a listing of the resource.
*
* @return Application|Factory|View
*/
public function index()
{
return view('admin.vouchers.index');
}

/**
* Show the form for creating a new resource.
*
* @return Application|Factory|View
*/
public function create()
{
return view('admin.vouchers.create');
}

/**
* Store a newly created resource in storage.
*
* @param Request $request
* @return RedirectResponse
*/
public function store(Request $request)
{
$request->validate([
'memo' => 'nullable|string|max:191',
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
'expires_at' => ['nullable','date_format:d-m-Y','after:today',"before:10 years"],
]);

Voucher::create($request->except('_token'));

return redirect()->route('admin.vouchers.index')->with('success', 'voucher has been created!');
}

/**
* Display the specified resource.
*
* @param Voucher $voucher
* @return Response
*/
public function show(Voucher $voucher)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param Voucher $voucher
* @return Application|Factory|View
*/
public function edit(Voucher $voucher)
{
return view('admin.vouchers.edit' , [
'voucher' => $voucher
]);
}

/**
* Update the specified resource in storage.
*
* @param Request $request
* @param Voucher $voucher
* @return RedirectResponse
*/
public function update(Request $request, Voucher $voucher)
{
$request->validate([
'memo' => 'nullable|string|max:191',
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
'expires_at' => ['nullable','date_format:d-m-Y','after:today',"before:10 years"],
]);

$voucher->update($request->except('_token'));

return redirect()->route('admin.vouchers.index')->with('success', 'voucher has been updated!');
}

/**
* Remove the specified resource from storage.
*
* @param Voucher $voucher
* @return RedirectResponse
*/
public function destroy(Voucher $voucher)
{
$voucher->delete();
return redirect()->back()->with('success', 'voucher has been removed!');
}

/**
* @param Request $request
* @return JsonResponse
* @throws ValidationException
*/
public function redeem(Request $request)
{
#general validations
$request->validate([
'code' => 'required|exists:vouchers,code'
]);

#get voucher by code
$voucher = Voucher::where('code' , '=' , $request->input('code'))->firstOrFail();

#extra validations
if ($voucher->getStatus() == 'USES_LIMIT_REACHED') throw ValidationException::withMessages([
'code' => 'This voucher has reached the maximum amount of uses'
]);

if ($voucher->getStatus() == 'EXPIRED') throw ValidationException::withMessages([
'code' => 'This voucher has expired'
]);

if (!$request->user()->vouchers()->where('id' , '=' , $voucher->id)->get()->isEmpty()) throw ValidationException::withMessages([
'code' => 'You already redeemed this voucher code'
]);

if ($request->user()->credits + $voucher->credits >= 99999999) throw ValidationException::withMessages([
'code' => "You can't redeem this voucher because you would exceed the credit limit"
]);

#redeem voucher
$voucher->redeem($request->user());

return response()->json([
'success' => "{$voucher->credits} credits have been added to your balance!"
]);
}

public function dataTable()
{
$query = Voucher::query();

return datatables($query)
->addColumn('actions', function (Voucher $voucher) {
return '
<a data-content="Edit" data-toggle="popover" data-trigger="hover" data-placement="top" href="' . route('admin.vouchers.edit', $voucher->id) . '" class="btn btn-sm btn-info mr-1"><i class="fas fa-pen"></i></a>
<form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.vouchers.destroy', $voucher->id) . '">
' . csrf_field() . '
' . method_field("DELETE") . '
<button data-content="Delete" data-toggle="popover" data-trigger="hover" data-placement="top" class="btn btn-sm btn-danger mr-1"><i class="fas fa-trash"></i></button>
</form>
';
})
->addColumn('status', function (Voucher $voucher) {
$color = 'success';
if ($voucher->getStatus() != 'VALID') $color = 'danger';
return '<span class="badge badge-' . $color . '">' . $voucher->getStatus() . '</span>';
})
->editColumn('uses', function (Voucher $voucher) {
$userCount = $voucher->users()->count();
return "{$userCount} / {$voucher->uses}";
})
->editColumn('credits', function (Voucher $voucher) {
return number_format($voucher->credits, 2, '.', '');
})
->editColumn('expires_at', function (Voucher $voucher) {
if (!$voucher->expires_at) return "";
return $voucher->expires_at ? $voucher->expires_at->diffForHumans() : '';
})
->editColumn('code', function (Voucher $voucher) {
return "<code>{$voucher->code}</code>";
})
->rawColumns(['actions', 'code', 'status'])
->make();
}

}
31 changes: 31 additions & 0 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
Expand Down Expand Up @@ -37,4 +38,34 @@ public function __construct()
{
$this->middleware('guest')->except('logout');
}

public function login(Request $request)
{
$request->validate([
$this->username() => 'required|string',
'password' => 'required|string',
'g-recaptcha-response' => ['required','recaptcha'],
]);

// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if (method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);

return $this->sendLockoutResponse($request);
}

if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}

// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);

return $this->sendFailedLoginResponse($request);
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function index()
return view('profile.index')->with([
'user' => Auth::user(),
'credits_reward_after_verify_discord' => Configuration::getValueByKey('CREDITS_REWARD_AFTER_VERIFY_DISCORD'),
'discord_verify_command' => Configuration::getValueByKey('DISCORD_VERIFY_COMMAND')
'force_email_verification' => Configuration::getValueByKey('FORCE_EMAIL_VERIFICATION'),
'force_discord_verification' => Configuration::getValueByKey('FORCE_DISCORD_VERIFICATION'),
]);
}

Expand Down
49 changes: 49 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@
use App\Notifications\WelcomeMessage;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Spatie\Activitylog\Traits\CausesActivity;
use Spatie\Activitylog\Traits\LogsActivity;

/**
* Class User
* @package App\Models
*/
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, Notifiable, LogsActivity, CausesActivity;

/**
* @var string[]
*/
protected static $logAttributes = ['name', 'email'];

/**
* @var string[]
*/
protected static $ignoreChangedAttributes = [
'remember_token',
'credits',
Expand Down Expand Up @@ -68,6 +81,9 @@ class User extends Authenticatable implements MustVerifyEmail
'last_seen' => 'datetime',
];

/**
*
*/
public static function boot()
{
parent::boot();
Expand All @@ -89,24 +105,38 @@ public static function boot()
}
});

$user->vouchers()->detach();

Pterodactyl::client()->delete("/application/users/{$user->pterodactyl_id}");
});
}

/**
*
*/
public function sendEmailVerificationNotification()
{
$this->notify(new QueuedVerifyEmail);
}

/**
* @return string
*/
public function credits()
{
return number_format($this->credits, 2, '.', '');
}

/**
* @return string
*/
public function getAvatar(){
return "https://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email)));
}

/**
* @return string
*/
public function creditUsage()
{
$usage = 0;
Expand All @@ -118,6 +148,9 @@ public function creditUsage()
return number_format($usage, 2, '.', '');
}

/**
* @return array|string|string[]
*/
public function getVerifiedStatus(){
$status = '';
if ($this->hasVerifiedEmail()) $status .= 'email ';
Expand All @@ -126,15 +159,31 @@ public function getVerifiedStatus(){
return $status;
}

/**
* @return BelongsToMany
*/
public function vouchers(){
return $this->belongsToMany(Voucher::class);
}

/**
* @return HasOne
*/
public function discordUser(){
return $this->hasOne(DiscordUser::class);
}

/**
* @return HasMany
*/
public function servers()
{
return $this->hasMany(Server::class);
}

/**
* @return HasMany
*/
public function payments()
{
return $this->hasMany(Payment::class);
Expand Down
Loading

0 comments on commit af508de

Please sign in to comment.