first commit

This commit is contained in:
2025-09-01 15:06:58 +02:00
commit d8b89fc4fe
5702 changed files with 1021992 additions and 0 deletions
@@ -0,0 +1,21 @@
<?php
namespace Vanguard\Repositories\Country;
interface CountryRepository
{
/**
* Create $key => $value array for all available countries.
*
* @param string $column
* @param string $key
* @return mixed
*/
public function lists($column = 'name', $key = 'id');
/**
* Get all available countries.
* @return mixed
*/
public function all();
}
@@ -0,0 +1,24 @@
<?php
namespace Vanguard\Repositories\Country;
use Vanguard\Country;
class EloquentCountry implements CountryRepository
{
/**
* {@inheritdoc}
*/
public function lists($column = 'name', $key = 'id')
{
return Country::orderBy('name')->pluck($column, $key);
}
/**
* {@inheritdoc}
*/
public function all()
{
return Country::all();
}
}
@@ -0,0 +1,72 @@
<?php
namespace Vanguard\Repositories\Permission;
use Vanguard\Events\Permission\Created;
use Vanguard\Events\Permission\Deleted;
use Vanguard\Events\Permission\Updated;
use Vanguard\Permission;
use Cache;
class EloquentPermission implements PermissionRepository
{
/**
* {@inheritdoc}
*/
public function all()
{
return Permission::all();
}
/**
* {@inheritdoc}
*/
public function find($id)
{
return Permission::find($id);
}
/**
* {@inheritdoc}
*/
public function create(array $data)
{
$permission = Permission::create($data);
event(new Created($permission));
return $permission;
}
/**
* {@inheritdoc}
*/
public function update($id, array $data)
{
$permission = $this->find($id);
$permission->update($data);
Cache::flush();
event(new Updated($permission));
return $permission;
}
/**
* {@inheritdoc}
*/
public function delete($id)
{
$permission = $this->find($id);
event(new Deleted($permission));
$status = $permission->delete();
Cache::flush();
return $status;
}
}
@@ -0,0 +1,46 @@
<?php
namespace Vanguard\Repositories\Permission;
interface PermissionRepository
{
/**
* Get all system permissions.
*
* @return mixed
*/
public function all();
/**
* Finds the permission by given id.
*
* @param $id
* @return mixed
*/
public function find($id);
/**
* Creates new permission from provided data.
*
* @param array $data
* @return mixed
*/
public function create(array $data);
/**
* Updates specified permission.
*
* @param $id
* @param array $data
* @return mixed
*/
public function update($id, array $data);
/**
* Remove specified permission from repository.
*
* @param $id
* @return mixed
*/
public function delete($id);
}
+99
View File
@@ -0,0 +1,99 @@
<?php
namespace Vanguard\Repositories\Role;
use Vanguard\Events\Role\Created;
use Vanguard\Events\Role\Deleted;
use Vanguard\Events\Role\Updated;
use Vanguard\Role;
class EloquentRole implements RoleRepository
{
/**
* {@inheritdoc}
*/
public function all()
{
return Role::all();
}
/**
* {@inheritdoc}
*/
public function getAllWithUsersCount()
{
return Role::withCount('users')->get();
}
/**
* {@inheritdoc}
*/
public function find($id)
{
return Role::find($id);
}
/**
* {@inheritdoc}
*/
public function create(array $data)
{
$role = Role::create($data);
event(new Created($role));
return $role;
}
/**
* {@inheritdoc}
*/
public function update($id, array $data)
{
$role = $this->find($id);
$role->update($data);
event(new Updated($role));
return $role;
}
/**
* {@inheritdoc}
*/
public function delete($id)
{
$role = $this->find($id);
event(new Deleted($role));
return $role->delete();
}
/**
* {@inheritdoc}
*/
public function updatePermissions($roleId, array $permissions)
{
$role = $this->find($roleId);
$role->syncPermissions($permissions);
}
/**
* {@inheritdoc}
*/
public function lists($column = 'name', $key = 'id')
{
return Role::pluck($column, $key);
}
/**
* {@inheritdoc}
*/
public function findByName($name)
{
return Role::where('name', $name)->first();
}
}
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace Vanguard\Repositories\Role;
use Vanguard\Role;
interface RoleRepository
{
/**
* Get all system roles.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function all();
/**
* Lists all system roles into $key => $column value pairs.
*
* @param string $column
* @param string $key
* @return mixed
*/
public function lists($column = 'name', $key = 'id');
/**
* Get all system roles with number of users for each role.
*
* @return mixed
*/
public function getAllWithUsersCount();
/**
* Find system role by id.
*
* @param $id Role Id
* @return Role|null
*/
public function find($id);
/**
* Find role by name:
*
* @param $name
* @return mixed
*/
public function findByName($name);
/**
* Create new system role.
*
* @param array $data
* @return Role
*/
public function create(array $data);
/**
* Update specified role.
*
* @param $id Role Id
* @param array $data
* @return Role
*/
public function update($id, array $data);
/**
* Remove role from repository.
*
* @param $id Role Id
* @return bool
*/
public function delete($id);
/**
* Update the permissions for given role.
*
* @param $roleId
* @param array $permissions
* @return mixed
*/
public function updatePermissions($roleId, array $permissions);
}
+99
View File
@@ -0,0 +1,99 @@
<?php
namespace Vanguard\Repositories\Session;
use Carbon\Carbon;
use Jenssegers\Agent\Agent;
use Vanguard\Repositories\User\UserRepository;
use DB;
class DbSession implements SessionRepository
{
/**
* @var UserRepository
*/
private $users;
/**
* @var Agent
*/
private $agent;
/**
* DbSession constructor.
* @param UserRepository $users
* @param Agent $agent
*/
public function __construct(UserRepository $users, Agent $agent)
{
$this->users = $users;
$this->agent = $agent;
}
/**
* {@inheritdoc}
*/
public function getUserSessions($userId)
{
$validTimestamp = Carbon::now()->subMinutes(config('session.lifetime'))->timestamp;
return DB::table('sessions')
->where('user_id', $userId)
->where('last_activity', '>=', $validTimestamp)
->get()
->map(function ($session) {
return $this->mapSessionAttributes($session);
});
}
private function mapSessionAttributes($session)
{
$this->agent->setUserAgent($session->user_agent);
$session->last_activity = Carbon::createFromTimestamp($session->last_activity);
$session->platform = $this->agent->platform();
$session->browser = $this->agent->browser();
$session->device = $this->agent->device();
return $session;
}
/**
* {@inheritdoc}
*/
public function invalidateSession($sessionId)
{
$user = $this->users->findBySessionId($sessionId);
DB::table('sessions')
->where('id', $sessionId)
->delete();
$this->users->update($user->id, ['remember_token' => null]);
}
/**
* {@inheritdoc}
*/
public function find($sessionId)
{
$session = DB::table('sessions')
->where('id', $sessionId)
->first();
return $session
? $this->mapSessionAttributes($session)
: null;
}
/**
* {@inheritdoc}
*/
public function invalidateAllSessionsForUser($userId)
{
DB::table('sessions')
->where('user_id', $userId)
->delete();
$this->users->update($userId, ['remember_token' => null]);
}
}
@@ -0,0 +1,36 @@
<?php
namespace Vanguard\Repositories\Session;
interface SessionRepository
{
/**
* Find session by id.
* @param $sessionId
* @return mixed
*/
public function find($sessionId);
/**
* Get all active sessions for specified user.
*
* @param $userId
* @return mixed
*/
public function getUserSessions($userId);
/**
* Invalidate specified session for provided user
*
* @param $sessionId
* @return mixed
*/
public function invalidateSession($sessionId);
/**
* Invalidate all sessions for user with given id.
* @param $userId
* @return mixed
*/
public function invalidateAllSessionsForUser($userId);
}
+271
View File
@@ -0,0 +1,271 @@
<?php
namespace Vanguard\Repositories\User;
use Vanguard\Http\Filters\UserKeywordSearch;
use Vanguard\Repositories\Role\RoleRepository;
use Vanguard\Role;
use Vanguard\Services\Auth\Social\ManagesSocialAvatarSize;
use Vanguard\Services\Upload\UserAvatarManager;
use Vanguard\User;
use Carbon\Carbon;
use DB;
use Illuminate\Database\SQLiteConnection;
use Laravel\Socialite\Contracts\User as SocialUser;
class EloquentUser implements UserRepository
{
use ManagesSocialAvatarSize;
/**
* @var UserAvatarManager
*/
private $avatarManager;
/**
* @var RoleRepository
*/
private $roles;
public function __construct(UserAvatarManager $avatarManager, RoleRepository $roles)
{
$this->avatarManager = $avatarManager;
$this->roles = $roles;
}
/**
* {@inheritdoc}
*/
public function find($id)
{
return User::find($id);
}
/**
* {@inheritdoc}
*/
public function findByEmail($email)
{
return User::where('email', $email)->first();
}
/**
* {@inheritdoc}
*/
public function findBySocialId($provider, $providerId)
{
return User::leftJoin('social_logins', 'users.id', '=', 'social_logins.user_id')
->select('users.*')
->where('social_logins.provider', $provider)
->where('social_logins.provider_id', $providerId)
->first();
}
/**
* {@inheritdoc}
*/
public function findBySessionId($sessionId)
{
return User::leftJoin('sessions', 'users.id', '=', 'sessions.user_id')
->select('users.*')
->where('sessions.id', $sessionId)
->first();
}
/**
* {@inheritdoc}
*/
public function create(array $data)
{
return User::create($data);
}
/**
* {@inheritdoc}
*/
public function associateSocialAccountForUser($userId, $provider, SocialUser $user)
{
return DB::table('social_logins')->insert([
'user_id' => $userId,
'provider' => $provider,
'provider_id' => $user->getId(),
'avatar' => $this->getAvatarForProvider($provider, $user),
'created_at' => Carbon::now()
]);
}
/**
* {@inheritdoc}
*/
public function paginate($perPage, $search = null, $status = null)
{
$query = User::query();
if ($status) {
$query->where('status', $status);
}
if ($search) {
(new UserKeywordSearch)($query, $search);
}
$result = $query->orderBy('id', 'desc')
->paginate($perPage);
if ($search) {
$result->appends(['search' => $search]);
}
if ($status) {
$result->appends(['status' => $status]);
}
return $result;
}
/**
* {@inheritdoc}
*/
public function update($id, array $data)
{
if (isset($data['country_id']) && $data['country_id'] == 0) {
$data['country_id'] = null;
}
$user = $this->find($id);
$user->update($data);
return $user;
}
/**
* {@inheritdoc}
*/
public function delete($id)
{
$user = $this->find($id);
$this->avatarManager->deleteAvatarIfUploaded($user);
return $user->delete();
}
/**
* {@inheritdoc}
*/
public function count()
{
return User::count();
}
/**
* {@inheritdoc}
*/
public function newUsersCount()
{
return User::whereBetween('created_at', [Carbon::now()->firstOfMonth(), Carbon::now()])
->count();
}
/**
* {@inheritdoc}
*/
public function countByStatus($status)
{
return User::where('status', $status)->count();
}
/**
* {@inheritdoc}
*/
public function latest($count = 20)
{
return User::orderBy('created_at', 'DESC')
->limit($count)
->get();
}
/**
* {@inheritdoc}
*/
public function countOfNewUsersPerMonth(Carbon $from, Carbon $to)
{
$result = User::whereBetween('created_at', [$from, $to])
->orderBy('created_at')
->get(['created_at'])
->groupBy(function ($user) {
return $user->created_at->format("Y_n");
});
$counts = [];
while ($from->lt($to)) {
$key = $from->format("Y_n");
$counts[$this->parseDate($key)] = count($result->get($key, []));
$from->addMonth();
}
return $counts;
}
/**
* Parse date from "Y_m" format to "{Month Name} {Year}" format.
* @param $yearMonth
* @return string
*/
private function parseDate($yearMonth)
{
list($year, $month) = explode("_", $yearMonth);
$month = trans("app.months.{$month}");
return "{$month} {$year}";
}
/**
* {@inheritdoc}
*/
public function getUsersWithRole($roleName)
{
return Role::where('name', $roleName)
->first()
->users;
}
/**
* {@inheritdoc}
*/
public function getUserSocialLogins($userId)
{
return DB::table('social_logins')
->where('user_id', $userId)
->get();
}
/**
* {@inheritdoc}
*/
public function setRole($userId, $roleId)
{
return $this->find($userId)->setRole($roleId);
}
/**
* {@inheritdoc}
*/
public function findByConfirmationToken($token)
{
return User::where('confirmation_token', $token)->first();
}
/**
* {@inheritdoc}
*/
public function switchRolesForUsers($fromRoleId, $toRoleId)
{
return User::where('role_id', $fromRoleId)
->update(['role_id' => $toRoleId]);
}
}
+171
View File
@@ -0,0 +1,171 @@
<?php
namespace Vanguard\Repositories\User;
use Carbon\Carbon;
use Vanguard\User;
use \Laravel\Socialite\Contracts\User as SocialUser;
interface UserRepository
{
/**
* Paginate registered users.
*
* @param $perPage
* @param null $search
* @param null $status
* @return mixed
*/
public function paginate($perPage, $search = null, $status = null);
/**
* Find user by its id.
*
* @param $id
* @return null|User
*/
public function find($id);
/**
* Find user by email.
*
* @param $email
* @return null|User
*/
public function findByEmail($email);
/**
* Find user registered via social network.
*
* @param $provider string Provider used for authentication.
* @param $providerId string Provider's unique identifier for authenticated user.
* @return mixed
*/
public function findBySocialId($provider, $providerId);
/**
* Find user by specified session id.
*
* @param $sessionId
* @return mixed
*/
public function findBySessionId($sessionId);
/**
* Create new user.
*
* @param array $data
* @return mixed
*/
public function create(array $data);
/**
* Update user specified by it's id.
*
* @param $id
* @param array $data
* @return User
*/
public function update($id, array $data);
/**
* Delete user with provided id.
*
* @param $id
* @return mixed
*/
public function delete($id);
/**
* Associate account details returned from social network
* to user with provided user id.
*
* @param $userId
* @param $provider
* @param SocialUser $user
* @return mixed
*/
public function associateSocialAccountForUser($userId, $provider, SocialUser $user);
/**
* Number of users in database.
*
* @return mixed
*/
public function count();
/**
* Number of users registered during current month.
*
* @return mixed
*/
public function newUsersCount();
/**
* Number of users with provided status.
*
* @param $status
* @return mixed
*/
public function countByStatus($status);
/**
* Count of registered users for every month within the
* provided date range.
*
* @param $from
* @param $to
* @return mixed
*/
public function countOfNewUsersPerMonth(Carbon $from, Carbon $to);
/**
* Get latest {$count} users from database.
*
* @param $count
* @return mixed
*/
public function latest($count = 20);
/**
* Set specified role to specified user.
*
* @param $userId
* @param $roleId
* @return mixed
*/
public function setRole($userId, $roleId);
/**
* Change role for all users who has role $fromRoleId to $toRoleId.
*
* @param $fromRoleId Id of current role.
* @param $toRoleId Id of new role.
* @return mixed
*/
public function switchRolesForUsers($fromRoleId, $toRoleId);
/**
* Get all users with provided role.
*
* @param $roleName
* @return mixed
*/
public function getUsersWithRole($roleName);
/**
* Get all social login records for specified user.
*
* @param $userId
* @return mixed
*/
public function getUserSocialLogins($userId);
/**
* Find user by confirmation token.
*
* @param $token
* @return mixed
*/
public function findByConfirmationToken($token);
}