feat: call appstore user lifecycle hooks from main app

This commit is contained in:
kuaifan 2025-12-09 10:30:23 +00:00
parent c668340661
commit ac9e1e5e67
3 changed files with 61 additions and 2 deletions

View File

@ -37,6 +37,7 @@ use App\Models\UserRecentItem;
use App\Models\UserTag; use App\Models\UserTag;
use App\Models\UserTagRecognition; use App\Models\UserTagRecognition;
use App\Models\UserAppSort; use App\Models\UserAppSort;
use App\Module\Apps;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use App\Models\UserEmailVerification; use App\Models\UserEmailVerification;
use App\Module\AgoraIO\AgoraTokenGenerator; use App\Module\AgoraIO\AgoraTokenGenerator;
@ -1098,6 +1099,8 @@ class UsersController extends AbstractController
$upArray = []; $upArray = [];
$upLdap = []; $upLdap = [];
$transferUser = null; $transferUser = null;
$hookAction = '';
$hookEvent = '';
switch ($type) { switch ($type) {
case 'setadmin': case 'setadmin':
$msg = '设置成功'; $msg = '设置成功';
@ -1179,12 +1182,16 @@ class UsersController extends AbstractController
return Base::retError('交接人已离职,请选择另一个交接人'); return Base::retError('交接人已离职,请选择另一个交接人');
} }
} }
$hookAction = 'user_offboard';
$hookEvent = 'offboard';
break; break;
case 'cleardisable': case 'cleardisable':
$msg = '操作成功'; $msg = '操作成功';
$upArray['identity'] = array_diff($userInfo->identity, ['disable']); $upArray['identity'] = array_diff($userInfo->identity, ['disable']);
$upArray['disable_at'] = null; $upArray['disable_at'] = null;
$hookAction = 'user_onboard';
$hookEvent = 'restore';
break; break;
case 'delete': case 'delete':
@ -1303,6 +1310,9 @@ class UsersController extends AbstractController
} }
}); });
} }
if ($hookAction) {
Apps::dispatchUserHook($userInfo, $hookAction, $hookEvent);
}
// //
return Base::retSuccess($msg, $userInfo); return Base::retSuccess($msg, $userInfo);
} }

View File

@ -5,6 +5,7 @@ namespace App\Models;
use App\Exceptions\ApiException; use App\Exceptions\ApiException;
use App\Module\Base; use App\Module\Base;
use App\Module\Doo; use App\Module\Doo;
use App\Module\Apps;
use App\Module\Table\OnlineData; use App\Module\Table\OnlineData;
use App\Services\RequestContext; use App\Services\RequestContext;
use Cache; use Cache;
@ -313,7 +314,7 @@ class User extends AbstractModel
*/ */
public function deleteUser($reason) public function deleteUser($reason)
{ {
return AbstractModel::transaction(function () use ($reason) { $ret = AbstractModel::transaction(function () use ($reason) {
// 删除原因 // 删除原因
$userDelete = UserDelete::createInstance([ $userDelete = UserDelete::createInstance([
'operator' => User::userid(), 'operator' => User::userid(),
@ -334,6 +335,10 @@ class User extends AbstractModel
// //
return $this->delete(); return $this->delete();
}); });
if ($ret) {
Apps::dispatchUserHook($this, 'user_offboard', 'delete');
}
return $ret;
} }
/** /**
@ -407,7 +412,9 @@ class User extends AbstractModel
$dialog?->joinGroup($user->userid, 0); $dialog?->joinGroup($user->userid, 0);
} }
} }
return $user->find($user->userid); $createdUser = $user->find($user->userid);
Apps::dispatchUserHook($createdUser, 'user_onboard', 'onboard');
return $createdUser;
} }
/** /**

View File

@ -3,8 +3,11 @@
namespace App\Module; namespace App\Module;
use App\Exceptions\ApiException; use App\Exceptions\ApiException;
use App\Models\User;
use App\Services\RequestContext; use App\Services\RequestContext;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use App\Module\Base;
use App\Module\Ihttp;
class Apps class Apps
{ {
@ -57,4 +60,43 @@ class Apps
throw new ApiException("应用「{$name}」未安装", [], 0, false); throw new ApiException("应用「{$name}」未安装", [], 0, false);
} }
} }
/**
* Dispatch user lifecycle hook to appstore (onboard/offboard/delete/restore).
*/
public static function dispatchUserHook(User $user, string $action, string $eventType = ''): void
{
$appKey = env('APP_KEY', '');
if (empty($appKey)) {
info('[appstore_hook] APP_KEY is empty, skip dispatchUserHook');
return;
}
$url = sprintf('http://appstore/api/v1/internal/hooks/%s', $action);
$payload = [
'user' => [
'id' => (string) $user->userid,
'email' => (string) $user->email,
'name' => (string) $user->nickname,
'role' => in_array('admin', $user->identity ?? []) ? 'admin' : 'normal',
],
];
if ($eventType !== '') {
$payload['event_type'] = $eventType;
}
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . md5($appKey),
];
$resp = Ihttp::ihttp_request($url, json_encode($payload, JSON_UNESCAPED_UNICODE), $headers, 5);
if (Base::isError($resp)) {
info('[appstore_hook] dispatch fail', [
'url' => $url,
'payload' => $payload,
'error' => $resp,
]);
}
}
} }