OneBot: Leantime 改造版源码(BOM/Univer 表格/AI 接管/品牌替换等)
This commit is contained in:
106
app/Domain/Auth/Guards/ApiGuard.php
Normal file
106
app/Domain/Auth/Guards/ApiGuard.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace Leantime\Domain\Auth\Guards;
|
||||
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Contracts\Auth\UserProvider;
|
||||
use Leantime\Core\Http\ApiRequest;
|
||||
use Leantime\Core\Http\IncomingRequest;
|
||||
use Leantime\Domain\Api\Services\Api;
|
||||
use Leantime\Domain\Auth\Models\AuthenticatableUser;
|
||||
|
||||
class ApiGuard implements Guard
|
||||
{
|
||||
protected $user;
|
||||
|
||||
private string $apiKey = '';
|
||||
|
||||
public function __construct(
|
||||
protected UserProvider $provider,
|
||||
protected Api $apiService,
|
||||
protected IncomingRequest $request)
|
||||
{
|
||||
if ($this->request instanceof ApiRequest && $this->request->isApiRequest()) {
|
||||
$this->apiKey = $this->request->getAPIKey();
|
||||
}
|
||||
}
|
||||
|
||||
public function check()
|
||||
{
|
||||
|
||||
if (empty($this->apiKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$apiUser = $this->apiService->getAPIKeyUser($this->apiKey);
|
||||
|
||||
if (! $apiUser) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function guest()
|
||||
{
|
||||
return ! $this->check();
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
if ($this->user !== null) {
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
if (empty($this->apiKey)) {
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
$apiUser = $this->apiService->getAPIKeyUser($this->apiKey);
|
||||
|
||||
if (! $apiUser) {
|
||||
$this->user = null;
|
||||
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
$this->user = new AuthenticatableUser((array) $apiUser);
|
||||
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function id()
|
||||
{
|
||||
return $this->user()?->getAuthIdentifier();
|
||||
}
|
||||
|
||||
public function validate(array $credentials = [])
|
||||
{
|
||||
|
||||
if (empty($this->apiKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$apiUser = $this->apiService->getAPIKeyUser($this->apiKey);
|
||||
|
||||
if (! $apiUser) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public function hasUser()
|
||||
{
|
||||
return $this->user ? true : false;
|
||||
}
|
||||
|
||||
public function setUser(Authenticatable $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
71
app/Domain/Auth/Guards/WebGuard.php
Normal file
71
app/Domain/Auth/Guards/WebGuard.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Leantime\Domain\Auth\Guards;
|
||||
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Contracts\Auth\UserProvider;
|
||||
use Leantime\Domain\Auth\Services\Auth as AuthService;
|
||||
|
||||
class WebGuard implements Guard
|
||||
{
|
||||
protected $provider;
|
||||
|
||||
protected $user;
|
||||
|
||||
protected AuthService $authService;
|
||||
|
||||
public function __construct(UserProvider $provider, AuthService $authService)
|
||||
{
|
||||
$this->provider = $provider;
|
||||
$this->authService = $authService;
|
||||
}
|
||||
|
||||
public function check()
|
||||
{
|
||||
return $this->authService->loggedIn();
|
||||
}
|
||||
|
||||
public function guest()
|
||||
{
|
||||
return ! $this->check();
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
if ($this->user !== null) {
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
if ($this->authService->loggedIn()) {
|
||||
$this->user = $this->provider->retrieveById($this->authService::getUserId());
|
||||
}
|
||||
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function hasUser()
|
||||
{
|
||||
return $this->user ? true : false;
|
||||
}
|
||||
|
||||
public function id()
|
||||
{
|
||||
return $this->user()?->getAuthIdentifier();
|
||||
}
|
||||
|
||||
public function validate(array $credentials = [])
|
||||
{
|
||||
return $this->authService->login(
|
||||
$credentials['username'],
|
||||
$credentials['password']
|
||||
);
|
||||
}
|
||||
|
||||
public function setUser(Authenticatable $user)
|
||||
{
|
||||
$this->user = $user;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user