namespace BookStack\Access;
+use BookStack\Users\Models\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;
-use Illuminate\Database\Eloquent\Model;
class ExternalBaseUserProvider implements UserProvider
{
- public function __construct(
- protected string $model
- ) {
- }
-
- /**
- * Create a new instance of the model.
- */
- public function createModel(): Model
- {
- $class = '\\' . ltrim($this->model, '\\');
-
- return new $class();
- }
-
/**
* Retrieve a user by their unique identifier.
*/
public function retrieveById(mixed $identifier): ?Authenticatable
{
- return $this->createModel()->newQuery()->find($identifier);
+ return User::query()->find($identifier);
}
/**
*/
public function retrieveByCredentials(array $credentials): ?Authenticatable
{
- // Search current user base by looking up a uid
- $model = $this->createModel();
-
- return $model->newQuery()
+ return User::query()
->where('external_auth_id', $credentials['external_auth_id'])
->first();
}
use BookStack\Access\RegistrationService;
use Illuminate\Auth\GuardHelpers;
-use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
+use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Session\Session;
* The name of the Guard. Typically "session".
*
* Corresponds to guard name in authentication configuration.
- *
- * @var string
*/
- protected $name;
+ protected readonly string $name;
/**
* The user we last attempted to retrieve.
- *
- * @var \Illuminate\Contracts\Auth\Authenticatable
*/
- protected $lastAttempted;
+ protected Authenticatable $lastAttempted;
/**
* The session used by the guard.
- *
- * @var \Illuminate\Contracts\Session\Session
*/
- protected $session;
+ protected Session $session;
/**
* Indicates if the logout method has been called.
- *
- * @var bool
*/
- protected $loggedOut = false;
+ protected bool $loggedOut = false;
/**
* Service to handle common registration actions.
- *
- * @var RegistrationService
*/
- protected $registrationService;
+ protected RegistrationService $registrationService;
/**
* Create a new authentication guard.
- *
- * @return void
*/
public function __construct(string $name, UserProvider $provider, Session $session, RegistrationService $registrationService)
{
/**
* Get the currently authenticated user.
- *
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
- public function user()
+ public function user(): Authenticatable|null
{
if ($this->loggedOut) {
- return;
+ return null;
}
// If we've already retrieved the user for the current request we can just
/**
* Get the ID for the currently authenticated user.
- *
- * @return int|null
*/
- public function id()
+ public function id(): int|null
{
if ($this->loggedOut) {
- return;
+ return null;
}
return $this->user()
/**
* Log a user into the application without sessions or cookies.
- *
- * @param array $credentials
- *
- * @return bool
*/
- public function once(array $credentials = [])
+ public function once(array $credentials = []): bool
{
if ($this->validate($credentials)) {
$this->setUser($this->lastAttempted);
/**
* Log the given user ID into the application without sessions or cookies.
- *
- * @param mixed $id
- *
- * @return \Illuminate\Contracts\Auth\Authenticatable|false
*/
- public function onceUsingId($id)
+ public function onceUsingId($id): Authenticatable|false
{
if (!is_null($user = $this->provider->retrieveById($id))) {
$this->setUser($user);
/**
* Validate a user's credentials.
- *
- * @param array $credentials
- *
- * @return bool
*/
- public function validate(array $credentials = [])
+ public function validate(array $credentials = []): bool
{
return false;
}
/**
* Attempt to authenticate a user using the given credentials.
- *
- * @param array $credentials
- * @param bool $remember
- *
- * @return bool
+ * @param bool $remember
*/
- public function attempt(array $credentials = [], $remember = false)
+ public function attempt(array $credentials = [], $remember = false): bool
{
return false;
}
/**
* Log the given user ID into the application.
- *
- * @param mixed $id
* @param bool $remember
- *
- * @return \Illuminate\Contracts\Auth\Authenticatable|false
*/
- public function loginUsingId($id, $remember = false)
+ public function loginUsingId(mixed $id, $remember = false): Authenticatable|false
{
// Always return false as to disable this method,
// Logins should route through LoginService.
/**
* Log a user into the application.
*
- * @param \Illuminate\Contracts\Auth\Authenticatable $user
- * @param bool $remember
- *
- * @return void
+ * @param bool $remember
*/
- public function login(AuthenticatableContract $user, $remember = false)
+ public function login(Authenticatable $user, $remember = false): void
{
$this->updateSession($user->getAuthIdentifier());
/**
* Update the session with the given ID.
- *
- * @param string $id
- *
- * @return void
*/
- protected function updateSession($id)
+ protected function updateSession(string|int $id): void
{
$this->session->put($this->getName(), $id);
/**
* Log the user out of the application.
- *
- * @return void
*/
- public function logout()
+ public function logout(): void
{
$this->clearUserDataFromStorage();
/**
* Remove the user data from the session and cookies.
- *
- * @return void
*/
- protected function clearUserDataFromStorage()
+ protected function clearUserDataFromStorage(): void
{
$this->session->remove($this->getName());
}
/**
* Get the last user we attempted to authenticate.
- *
- * @return \Illuminate\Contracts\Auth\Authenticatable
*/
- public function getLastAttempted()
+ public function getLastAttempted(): Authenticatable
{
return $this->lastAttempted;
}
/**
* Get a unique identifier for the auth session value.
- *
- * @return string
*/
- public function getName()
+ public function getName(): string
{
return 'login_' . $this->name . '_' . sha1(static::class);
}
/**
* Determine if the user was authenticated via "remember me" cookie.
- *
- * @return bool
*/
- public function viaRemember()
+ public function viaRemember(): bool
{
return false;
}
/**
* Return the currently cached user.
- *
- * @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
- public function getUser()
+ public function getUser(): Authenticatable|null
{
return $this->user;
}
/**
* Set the current user.
- *
- * @param \Illuminate\Contracts\Auth\Authenticatable $user
- *
- * @return $this
*/
- public function setUser(AuthenticatableContract $user)
+ public function setUser(Authenticatable $user): self
{
$this->user = $user;
/**
* Get the parent comment this is in reply to (if existing).
- * @return BelongsTo<Comment, Comment>
+ * @return BelongsTo<Comment, $this>
*/
public function parent(): BelongsTo
{
*/
public function register(): void
{
- Auth::provider('external-users', function ($app, array $config) {
- return new ExternalBaseUserProvider($config['model']);
+ Auth::provider('external-users', function () {
+ return new ExternalBaseUserProvider();
});
// Bind and provide the default system user as a singleton to the app instance when needed.
/**
* The event listener mappings for the application.
*
- * @var array<class-string, array<int, class-string>>
+ * @var array<class-string, array<int, string>>
*/
protected $listen = [
SocialiteWasCalled::class => [
throw new NotFoundException();
}
- $prev = $revision->getPrevious();
+ $prev = $revision->getPreviousRevision();
$prevContent = $prev->html ?? '';
$diff = Diff::excecute($prevContent, $revision->html);
/**
* Get the previous revision for the same page if existing.
*/
- public function getPrevious(): ?PageRevision
+ public function getPreviousRevision(): ?PageRevision
{
$id = static::newQuery()->where('page_id', '=', $this->page_id)
->where('id', '<', $this->id)
* If the callable returns a response, this response will be returned
* to the request upon error.
*/
- public function prepareForOutOfMemory(callable $onOutOfMemory)
+ public function prepareForOutOfMemory(callable $onOutOfMemory): void
{
$this->onOutOfMemory = $onOutOfMemory;
}
/**
* Forget the current out of memory handler, if existing.
*/
- public function forgetOutOfMemoryHandler()
+ public function forgetOutOfMemoryHandler(): void
{
$this->onOutOfMemory = null;
}
return $this->queryVisible()->get();
}
+ /**
+ * @return Builder<Import>
+ */
public function queryVisible(): Builder
{
$query = Import::query();
use BookStack\Exports\ZipExports\ZipValidationHelper;
use BookStack\Uploads\Attachment;
-class ZipExportAttachment extends ZipExportModel
+final class ZipExportAttachment extends ZipExportModel
{
public ?int $id = null;
public string $name;
return $context->validateData($data, $rules);
}
- public static function fromArray(array $data): self
+ public static function fromArray(array $data): static
{
- $model = new self();
+ $model = new static();
$model->id = $data['id'] ?? null;
$model->name = $data['name'];
use BookStack\Exports\ZipExports\ZipExportFiles;
use BookStack\Exports\ZipExports\ZipValidationHelper;
-class ZipExportBook extends ZipExportModel
+final class ZipExportBook extends ZipExportModel
{
public ?int $id = null;
public string $name;
return $errors;
}
- public static function fromArray(array $data): self
+ public static function fromArray(array $data): static
{
- $model = new self();
+ $model = new static();
$model->id = $data['id'] ?? null;
$model->name = $data['name'];
use BookStack\Exports\ZipExports\ZipExportFiles;
use BookStack\Exports\ZipExports\ZipValidationHelper;
-class ZipExportChapter extends ZipExportModel
+final class ZipExportChapter extends ZipExportModel
{
public ?int $id = null;
public string $name;
return $errors;
}
- public static function fromArray(array $data): self
+ public static function fromArray(array $data): static
{
- $model = new self();
+ $model = new static();
$model->id = $data['id'] ?? null;
$model->name = $data['name'];
use BookStack\Uploads\Image;
use Illuminate\Validation\Rule;
-class ZipExportImage extends ZipExportModel
+final class ZipExportImage extends ZipExportModel
{
public ?int $id = null;
public string $name;
return $context->validateData($data, $rules);
}
- public static function fromArray(array $data): self
+ public static function fromArray(array $data): static
{
- $model = new self();
+ $model = new static();
$model->id = $data['id'] ?? null;
$model->name = $data['name'];
/**
* Decode the array of data into this export model.
*/
- abstract public static function fromArray(array $data): self;
+ abstract public static function fromArray(array $data): static;
/**
* Decode an array of array data into an array of export models.
* @param array[] $data
- * @return self[]
+ * @return static[]
*/
public static function fromManyArray(array $data): array
{
use BookStack\Exports\ZipExports\ZipExportFiles;
use BookStack\Exports\ZipExports\ZipValidationHelper;
-class ZipExportPage extends ZipExportModel
+final class ZipExportPage extends ZipExportModel
{
public ?int $id = null;
public string $name;
return $errors;
}
- public static function fromArray(array $data): self
+ public static function fromArray(array $data): static
{
- $model = new self();
+ $model = new static();
$model->id = $data['id'] ?? null;
$model->name = $data['name'];
use BookStack\Activity\Models\Tag;
use BookStack\Exports\ZipExports\ZipValidationHelper;
-class ZipExportTag extends ZipExportModel
+final class ZipExportTag extends ZipExportModel
{
public string $name;
public ?string $value = null;
return $context->validateData($data, $rules);
}
- public static function fromArray(array $data): self
+ public static function fromArray(array $data): static
{
- $model = new self();
+ $model = new static();
$model->name = $data['name'];
$model->value = $data['value'] ?? null;
/**
* The application's global HTTP middleware stack.
* These middleware are run during every request to your application.
+ *
+ * @var list<class-string>
*/
protected $middleware = [
\BookStack\Http\Middleware\PreventRequestsDuringMaintenance::class,
/**
* The application's route middleware groups.
*
- * @var array
+ * @var array<string, array<int, class-string>>
*/
protected $middlewareGroups = [
'web' => [
/**
* The application's middleware aliases.
*
- * @var array
+ * @var array<string, class-string>
*/
protected $middlewareAliases = [
'auth' => \BookStack\Http\Middleware\Authenticate::class,
/**
* The names of the cookies that should not be encrypted.
*
- * @var array
+ * @var array<int, string>
*/
protected $except = [
//
/**
* The URIs that should be reachable while maintenance mode is enabled.
*
- * @var array
+ * @var array<int, string>
*/
protected $except = [
//
/**
* The names of the attributes that should not be trimmed.
*
- * @var array
+ * @var array<int, string>
*/
protected $except = [
'password',
/**
* The trusted proxies for this application.
*
- * @var array
+ * @var array<int,string>|string|null
*/
protected $proxies;
/**
* The URIs that should be excluded from CSRF verification.
*
- * @var array
+ * @var array<int, string>
*/
protected $except = [
'saml2/*',
*/
protected array $options = [];
+ /**
+ * @param T[] $options
+ */
public function __construct(array $options = [])
{
$this->options = $options;
*
* @param array<string, int> $termCounts
*
- * @return array<string, int>
+ * @return array<string, float>
*/
protected function rawTermCountsToAdjustments(array $termCounts): array
{
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
/**
* The attributes that are mass assignable.
*
- * @var array
+ * @var list<string>
*/
protected $fillable = ['name', 'email'];
/**
* The attributes excluded from the model's JSON form.
*
- * @var array
+ * @var list<string>
*/
protected $hidden = [
'password', 'remember_token', 'system_name', 'email_confirmed', 'external_auth_id', 'email',
/**
* The roles that belong to the user.
*
- * @return BelongsToMany
+ * @return BelongsToMany<Role, $this>
*/
- public function roles()
+ public function roles(): BelongsToMany
{
- if ($this->id === 0) {
- return;
- }
-
return $this->belongsToMany(Role::class);
}
}
/**
- * Forget the handler so no action is taken place on out of memory.
+ * Forget the handler, so no action is taken place on out of memory.
*/
public function forget(): void
{
protected function getHandler(): Handler
{
+ /**
+ * We want to resolve our specific BookStack handling via the set app handler
+ * singleton, but phpstan will only infer based on the interface.
+ * @phpstan-ignore return.type
+ */
return app()->make(ExceptionHandler::class);
}
}