Skip to content

Commit

Permalink
Merge pull request #449 from ControlPanel-gg/development
Browse files Browse the repository at this point in the history
v. 0.7.6
  • Loading branch information
1day2die authored Jun 13, 2022
2 parents f1e5b43 + 4e63d53 commit 597d283
Show file tree
Hide file tree
Showing 32 changed files with 746 additions and 205 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- PayPal Integration
- Stripe Integration
- Referral System
- Email Verification
- Audit Log
- Admin Dashboard
Expand Down
11 changes: 11 additions & 0 deletions app/Classes/Settings/Misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function updateSettings(Request $request)
'mailencryption' => 'nullable|string',
'mailfromadress' => 'nullable|string',
'mailfromname' => 'nullable|string',
'enable_referral' => 'nullable|string',
'referral_reward' => 'nullable|numeric',
'referral_allowed' => 'nullable|string',
'referral_percentage' => 'nullable|numeric',
'referral_mode' => 'nullable|string',
]);

if ($validator->fails()) {
Expand Down Expand Up @@ -69,6 +74,12 @@ public function updateSettings(Request $request)
"SETTINGS::MAIL:ENCRYPTION" => "mailencryption",
"SETTINGS::MAIL:FROM_ADDRESS" => "mailfromadress",
"SETTINGS::MAIL:FROM_NAME" => "mailfromname",
"SETTINGS::REFERRAL::ENABLED" => "enable_referral",
"SETTINGS::REFERRAL::REWARD" => "referral_reward",
"SETTINGS::REFERRAL::ALLOWED" => "referral_allowed",
"SETTINGS::REFERRAL:MODE" => "referral_mode",
"SETTINGS::REFERRAL:PERCENTAGE" => "referral_percentage"


];

Expand Down
56 changes: 53 additions & 3 deletions app/Http/Controllers/Admin/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\InvoiceItem;
Expand Down Expand Up @@ -179,9 +181,25 @@ public function PaypalSuccess(Request $laravelRequest)
}


//update role
//update role give Referral-reward
if ($user->role == 'member') {
$user->update(['role' => 'client']);

if((config("SETTINGS::REFERRAL:MODE") == "commission" || config("SETTINGS::REFERRAL:MODE") == "both") && $shopProduct->type=="Credits"){
if($ref_user = DB::table("user_referrals")->where('registered_user_id', '=', $user->id)->first()){
$ref_user = User::findOrFail($ref_user->referral_id);
$increment = number_format($shopProduct->quantity/100*config("SETTINGS::REFERRAL:PERCENTAGE"),0,"","");
$ref_user->increment('credits', $increment);

//LOGS REFERRALS IN THE ACTIVITY LOG
activity()
->performedOn($user)
->causedBy($ref_user)
->log('gained '. $increment.' '.config("SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME").' for commission-referral of '.$user->name.' (ID:'.$user->id.')');
}

}

}

//store payment
Expand Down Expand Up @@ -326,9 +344,25 @@ public function StripeSuccess(Request $request)
$user->increment('server_limit', $shopProduct->quantity);
}

//update role
//update role give Referral-reward
if ($user->role == 'member') {
$user->update(['role' => 'client']);

if((config("SETTINGS::REFERRAL:MODE") == "commission" || config("SETTINGS::REFERRAL:MODE") == "both") && $shopProduct->type=="Credits"){
if($ref_user = DB::table("user_referrals")->where('registered_user_id', '=', $user->id)->first()){
$ref_user = User::findOrFail($ref_user->referral_id);
$increment = number_format($shopProduct->quantity/100*config("SETTINGS::REFERRAL:PERCENTAGE"),0,"","");
$ref_user->increment('credits', $increment);

//LOGS REFERRALS IN THE ACTIVITY LOG
activity()
->performedOn($user)
->causedBy($ref_user)
->log('gained '. $increment.' '.config("SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME").' for commission-referral of '.$user->name.' (ID:'.$user->id.')');
}

}

}

//store paid payment
Expand Down Expand Up @@ -431,9 +465,25 @@ protected function handleStripePaymentSuccessHook($paymentIntent)
$user->increment('server_limit', $shopProduct->quantity);
}

//update role
//update role give Referral-reward
if ($user->role == 'member') {
$user->update(['role' => 'client']);

if((config("SETTINGS::REFERRAL:MODE") == "commission" || config("SETTINGS::REFERRAL:MODE") == "both")&& $shopProduct->type=="Credits"){
if($ref_user = DB::table("user_referrals")->where('registered_user_id', '=', $user->id)->first()){
$ref_user = User::findOrFail($ref_user->referral_id);
$increment = number_format($shopProduct->quantity/100*config("SETTINGS::REFERRAL:PERCENTAGE"),0,"","");
$ref_user->increment('credits', $increment);

//LOGS REFERRALS IN THE ACTIVITY LOG
activity()
->performedOn($user)
->causedBy($ref_user)
->log('gained '. $increment.' '.config("SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME").' for commission-referral of '.$user->name.' (ID:'.$user->id.')');
}

}

}

//update payment db entry status
Expand Down
18 changes: 16 additions & 2 deletions app/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Settings;
use App\Models\User;
use App\Notifications\DynamicNotification;
use Illuminate\Support\Facades\DB;
use Spatie\QueryBuilder\QueryBuilder;
use Exception;
use Illuminate\Contracts\Foundation\Application;
Expand Down Expand Up @@ -52,8 +53,18 @@ public function index(Request $request)
*/
public function show(User $user)
{
//QUERY ALL REFERRALS A USER HAS
//i am not proud of this at all.
$allReferals = array();
$referrals = DB::table("user_referrals")->where("referral_id","=",$user->id)->get();
foreach($referrals as $referral){
array_push($allReferals, $allReferals["id"] = User::query()->findOrFail($referral->registered_user_id));
}
array_pop($allReferals);

return view('admin.users.show')->with([
'user' => $user
'user' => $user,
'referrals' => $allReferals
]);
}

Expand Down Expand Up @@ -258,6 +269,9 @@ public function dataTable()
->addColumn('servers', function (User $user) {
return $user->servers->count();
})
->addColumn('referrals', function (User $user) {
return DB::table('user_referrals')->where("referral_id","=",$user->id)->count();
})
->addColumn('discordId', function (User $user) {
return $user->discordUser ? $user->discordUser->id : '';
})
Expand Down Expand Up @@ -307,7 +321,7 @@ public function dataTable()
->orderColumn('last_seen', function ($query) {
$query->orderBy('last_seen', "desc");
})
->rawColumns(['avatar', 'name', 'credits', 'role', 'usage', 'actions', 'last_seen'])
->rawColumns(['avatar', 'name', 'credits', 'role', 'usage', 'referrals', 'actions', 'last_seen'])
->make(true);
}
}
34 changes: 33 additions & 1 deletion app/Http/Controllers/Api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use App\Models\DiscordUser;
use App\Models\Settings;
use App\Models\User;
use App\Notifications\ReferralNotification;
use Carbon\Carbon;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Routing\ResponseFactory;
Expand All @@ -17,6 +19,7 @@
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
Expand Down Expand Up @@ -227,7 +230,17 @@ public function unsuspend(Request $request, int $id)

return $user;
}

/**
* Create a unique Referral Code for User
* @return string
*/
protected function createReferralCode(){
$referralcode = STR::random(8);
if (User::where('referral_code', '=', $referralcode)->exists()) {
$this->createReferralCode();
}
return $referralcode;
}
/**
* @throws ValidationException
*/
Expand All @@ -245,6 +258,7 @@ public function store(Request $request)
'credits' => config('SETTINGS::USER:INITIAL_CREDITS', 150),
'server_limit' => config('SETTINGS::USER:INITIAL_SERVER_LIMIT', 1),
'password' => Hash::make($request->input('password')),
'referral_code' => $this->createReferralCode(),
]);

$response = Pterodactyl::client()->post('/application/users', [
Expand All @@ -269,7 +283,25 @@ public function store(Request $request)
$user->update([
'pterodactyl_id' => $response->json()['attributes']['id']
]);
//INCREMENT REFERRAL-USER CREDITS
if(!empty($request->input("referral_code"))){
$ref_code = $request->input("referral_code");
$new_user = $user->id;
if($ref_user = User::query()->where('referral_code', '=', $ref_code)->first()) {
if(config("SETTINGS::REFERRAL:MODE") == "register" || config("SETTINGS::REFERRAL:MODE") == "both") {
$ref_user->increment('credits', config("SETTINGS::REFERRAL::REWARD"));
$ref_user->notify(new ReferralNotification($ref_user->id, $new_user));
}
//INSERT INTO USER_REFERRALS TABLE
DB::table('user_referrals')->insert([
'referral_id' => $ref_user->id,
'registered_user_id' => $user->id,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
]);
}

}
$user->sendEmailVerificationNotification();

return $user;
Expand Down
43 changes: 43 additions & 0 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
use App\Http\Controllers\Controller;
use App\Models\Settings;
use App\Models\User;
use App\Notifications\ReferralNotification;
use App\Providers\RouteServiceProvider;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
Expand Down Expand Up @@ -78,6 +83,18 @@ protected function validator(array $data)
return Validator::make($data, $validationRules);
}

/**
* Create a unique Referral Code for User
* @return string
*/
protected function createReferralCode(){
$referralcode = STR::random(8);
if (User::where('referral_code', '=', $referralcode)->exists()) {
$this->createReferralCode();
}
return $referralcode;
}

/**
* Create a new user instance after a valid registration.
*
Expand All @@ -92,6 +109,8 @@ protected function create(array $data)
'credits' => config('SETTINGS::USER:INITIAL_CREDITS', 150),
'server_limit' => config('SETTINGS::USER:INITIAL_SERVER_LIMIT', 1),
'password' => Hash::make($data['password']),
'referral_code' => $this->createReferralCode(),

]);

$response = Pterodactyl::client()->post('/application/users', [
Expand All @@ -116,7 +135,31 @@ protected function create(array $data)
'pterodactyl_id' => $response->json()['attributes']['id']
]);

//INCREMENT REFERRAL-USER CREDITS
if(!empty($data['referral_code'])){
$ref_code = $data['referral_code'];
$new_user = $user->id;
if($ref_user = User::query()->where('referral_code', '=', $ref_code)->first()) {
if(config("SETTINGS::REFERRAL:MODE") == "sign-up" || config("SETTINGS::REFERRAL:MODE") == "both") {
$ref_user->increment('credits', config("SETTINGS::REFERRAL::REWARD"));
$ref_user->notify(new ReferralNotification($ref_user->id, $new_user));

//LOGS REFERRALS IN THE ACTIVITY LOG
activity()
->performedOn($user)
->causedBy($ref_user)
->log('gained '. config("SETTINGS::REFERRAL::REWARD").' '.config("SETTINGS::SYSTEM:CREDITS_DISPLAY_NAME").' for sign-up-referral of '.$user->name.' (ID:'.$user->id.')');
}
//INSERT INTO USER_REFERRALS TABLE
DB::table('user_referrals')->insert([
'referral_id' => $ref_user->id,
'registered_user_id' => $user->id,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
]);
}

}

return $user;
}
Expand Down
35 changes: 24 additions & 11 deletions app/Http/Controllers/Auth/SocialiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Settings;
use App\Models\User;
use App\Models\Voucher;
use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Http;
use Laravel\Socialite\Facades\Socialite;
Expand Down Expand Up @@ -35,17 +36,29 @@ public function callback()
$guildId = config("SETTINGS::DISCORD:GUILD_ID");
$roleId = config("SETTINGS::DISCORD:ROLE_ID");

//save / update discord_users
if (is_null($user->discordUser)) {
//create discord user in db
DiscordUser::create(array_merge($discord->user, ['user_id' => Auth::user()->id]));
//update user
Auth::user()->increment('credits', config('SETTINGS::USER:CREDITS_REWARD_AFTER_VERIFY_DISCORD'));
Auth::user()->increment('server_limit', config('SETTINGS::USER:SERVER_LIMIT_REWARD_AFTER_VERIFY_DISCORD'));
Auth::user()->update(['discord_verified_at' => now()]);
} else {
$user->discordUser->update($discord->user);
}
//save / update discord_users

//check if discord account is already linked to an cpgg account
if (is_null($user->discordUser)) {
$discordLinked = DiscordUser::where('id', '=', $discord->id)->first();
if ($discordLinked !== null) {
return redirect()->route('profile.index')->with(
'error',
'Discord account already linked!'
);
}

//create discord user in db
DiscordUser::create(array_merge($discord->user, ['user_id' => Auth::user()->id]));

//update user
Auth::user()->increment('credits', config('SETTINGS::USER:CREDITS_REWARD_AFTER_VERIFY_DISCORD'));
Auth::user()->increment('server_limit', config('SETTINGS::USER:SERVER_LIMIT_REWARD_AFTER_VERIFY_DISCORD'));
Auth::user()->update(['discord_verified_at' => now()]);

} else {
$user->discordUser->update($discord->user);
}

//force user into discord server
//TODO Add event on failure, to notify ppl involved
Expand Down
15 changes: 9 additions & 6 deletions app/Http/Controllers/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

namespace App\Http\Controllers;

use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Auth;

class NotificationController extends Controller
Expand All @@ -15,7 +10,6 @@ class NotificationController extends Controller
public function index()
{
$notifications = Auth::user()->notifications()->paginate();

return view('notifications.index')->with([
'notifications' => $notifications
]);
Expand All @@ -31,4 +25,13 @@ public function show(string $id)
'notification' => $notification
]);
}

public function readAll(){
$notifications = Auth::user()->notifications()->get();
foreach($notifications as $notification){
$notification->markAsRead();
}
return redirect()->back();

}
}
Loading

0 comments on commit 597d283

Please sign in to comment.