pixelfed/app/Listeners/AuthLogin.php

149 lines
4 KiB
PHP
Raw Permalink Normal View History

2018-07-23 17:29:20 +00:00
<?php
2018-09-16 00:36:04 +00:00
namespace App\Listeners;
2018-07-23 17:29:20 +00:00
2018-11-27 08:54:10 +00:00
use DB, Cache;
use App\{
Follower,
2018-12-16 02:54:47 +00:00
Profile,
2018-11-27 08:54:10 +00:00
User,
2019-03-06 07:55:03 +00:00
UserDevice,
2018-11-27 08:54:10 +00:00
UserFilter,
UserSetting
};
2018-09-16 00:36:04 +00:00
use Illuminate\Queue\InteractsWithQueue;
2018-12-16 02:54:47 +00:00
use App\Jobs\AvatarPipeline\CreateAvatar;
2018-09-16 00:36:04 +00:00
use Illuminate\Contracts\Queue\ShouldQueue;
2018-07-23 17:29:20 +00:00
2018-09-16 00:36:04 +00:00
class AuthLogin
2018-07-23 17:29:20 +00:00
{
2018-09-16 00:36:04 +00:00
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
2018-07-23 17:29:20 +00:00
{
2018-09-16 00:36:04 +00:00
$user = $event->user;
2018-12-21 19:54:14 +00:00
if(!$user) {
return;
}
2019-06-16 05:31:00 +00:00
$this->userProfile($user);
2019-03-06 07:55:03 +00:00
$this->userSettings($user);
$this->userState($user);
$this->userDevice($user);
2019-06-16 05:31:00 +00:00
$this->userProfileId($user);
$this->userLanguage($user);
2019-06-16 05:31:00 +00:00
}
protected function userProfile($user)
{
if (empty($user->profile)) {
2020-11-25 17:11:50 +00:00
if($user->created_at->lt(now()->subDays(1)) && empty($user->status)) {
2020-11-25 17:10:16 +00:00
$p = Profile::withTrashed()->whereUserId($user->id)->first();
if($p) {
$p->restore();
return;
}
}
2019-06-16 22:12:39 +00:00
DB::transaction(function() use($user) {
2019-06-21 18:54:58 +00:00
$profile = Profile::firstOrCreate(['user_id' => $user->id]);
if($profile->wasRecentlyCreated == true) {
$profile->username = $user->username;
$profile->name = $user->name;
$pkiConfig = [
'digest_alg' => 'sha512',
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
];
$pki = openssl_pkey_new($pkiConfig);
openssl_pkey_export($pki, $pki_private);
$pki_public = openssl_pkey_get_details($pki);
$pki_public = $pki_public['key'];
2019-06-16 05:31:00 +00:00
2019-06-21 18:54:58 +00:00
$profile->private_key = $pki_private;
$profile->public_key = $pki_public;
$profile->save();
2019-06-16 05:31:00 +00:00
2019-06-21 18:54:58 +00:00
CreateAvatar::dispatch($profile);
}
2019-06-16 05:31:00 +00:00
});
}
2019-03-06 07:55:03 +00:00
}
protected function userSettings($user)
{
2018-08-28 03:07:36 +00:00
if (empty($user->settings)) {
2018-10-24 18:41:14 +00:00
DB::transaction(function() use($user) {
UserSetting::firstOrCreate([
'user_id' => $user->id
]);
});
2018-07-23 17:29:20 +00:00
}
2019-03-06 07:55:03 +00:00
}
protected function userState($user)
{
2018-12-25 05:42:15 +00:00
if($user->status != null) {
$profile = $user->profile;
2019-01-08 02:31:55 +00:00
if(!$profile) {
return;
}
2018-12-25 05:42:15 +00:00
switch ($user->status) {
case 'disabled':
$profile->status = null;
$user->status = null;
$profile->save();
$user->save();
break;
case 'delete':
$profile->status = null;
$profile->delete_after = null;
$user->status = null;
$user->delete_after = null;
$profile->save();
$user->save();
break;
default:
# code...
break;
}
}
2018-07-23 17:29:20 +00:00
}
2019-03-06 07:55:03 +00:00
protected function userDevice($user)
{
$device = DB::transaction(function() use($user) {
return UserDevice::firstOrCreate([
'user_id' => $user->id,
'ip' => request()->ip(),
2019-06-21 18:54:58 +00:00
'user_agent' => str_limit(request()->userAgent(), 180),
2019-03-06 07:55:03 +00:00
]);
});
}
2019-06-16 05:31:00 +00:00
protected function userProfileId($user)
{
if($user->profile_id == null) {
DB::transaction(function() use($user) {
$profile = $user->profile;
2019-06-21 18:54:58 +00:00
if($profile) {
$user->profile_id = $profile->id;
$user->save();
}
2019-06-16 05:31:00 +00:00
});
}
}
protected function userLanguage($user)
{
session()->put('locale', $user->language ?? config('app.locale'));
}
2018-07-23 17:29:20 +00:00
}