Skip to content

Commit

Permalink
Merge pull request #7 from fintechbd/LP-28-Update-Permission
Browse files Browse the repository at this point in the history
LP-28 update permission repository working
  • Loading branch information
hafijul233 authored Oct 1, 2023
2 parents 1389841 + dd69f4a commit f4d92fc
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 239 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^8.1",
"fintech/core": "^0.0.1",
"fintech/core": "*",
"illuminate/contracts": "^10.0",
"laravel/sanctum": "^3.3",
"owen-it/laravel-auditing": "^13.5",
Expand Down
8 changes: 7 additions & 1 deletion src/Http/Requests/UpdatePermissionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fintech\Auth\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdatePermissionRequest extends FormRequest
{
Expand All @@ -21,8 +22,13 @@ public function authorize(): bool
*/
public function rules(): array
{
// dd($this->route('permission'));

$uniqueRule = 'unique:'.config('fintech.auth.permission_model', \Fintech\Auth\Models\Permission::class).',name,';

return [
//
'name' => ['required', 'string', 'min:5', 'max:255', $uniqueRule],
'guard_name' => ['required', 'string', Rule::in(array_keys(config('auth.guards', ['web', 'api'])))],
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/PermissionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function update(int|string $id, array $attributes = []);
*
* @throws PermissionRepositoryException
*/
public function read(int|string $id, $onlyTrashed = false);
public function find(int|string $id, $onlyTrashed = false);

/**
* find and delete a entry from records
Expand Down
162 changes: 3 additions & 159 deletions src/Repositories/Eloquent/PermissionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Fintech\Auth\Repositories\Eloquent;

use Fintech\Auth\Exceptions\PermissionRepositoryException;
use Fintech\Auth\Interfaces\PermissionRepository as InterfacesPermissionRepository;
use Fintech\Core\Repositories\EloquentRepository;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -13,10 +13,8 @@
/**
* Class PermissionRepository
*/
class PermissionRepository implements InterfacesPermissionRepository
class PermissionRepository extends EloquentRepository implements InterfacesPermissionRepository
{
private Model $model;

public function __construct()
{
$model = app()->make(config('fintech.auth.permission_model', \Fintech\Auth\Models\Permission::class));
Expand All @@ -39,8 +37,7 @@ public function list(array $filters = [])
$query = $this->model->newQuery();

if (isset($filters['search']) && ! empty($filters['search'])) {
$query->where('name', 'like', "%{$filters['search']}%")
->orWhere('guard_name', 'like', "%{$filters['search']}%");
$query->where('name', 'like', "%{$filters['search']}%");
}

//Handle Sorting
Expand All @@ -52,157 +49,4 @@ public function list(array $filters = [])
: $query->get();

}

/**
* Create a new entry resource
*
* @return Model|null
*
* @throws PermissionRepositoryException
*/
public function create(array $attributes = [])
{
try {
$this->model->fill($attributes);
if ($this->model->saveOrFail()) {

$this->model->refresh();

return $this->model;
}
} catch (\Throwable $e) {

throw new PermissionRepositoryException($e->getMessage(), 0, $e);
}

return null;
}

/**
* find and update a resource attributes
*
* @return Model|null
*
* @throws PermissionRepositoryException
*/
public function update(int|string $id, array $attributes = [])
{
try {

$this->model = $this->model->findOrFail($id);

} catch (\Throwable $exception) {

throw new ModelNotFoundException($exception->getMessage(), 0, $exception);
}

try {
if ($this->model->updateOrFail($attributes)) {

$this->model->refresh();

return $this->model;
}
} catch (\Throwable $exception) {

throw new PermissionRepositoryException($exception->getMessage(), 0, $exception);
}

return null;
}

/**
* find and delete a entry from records
*
* @param bool $onlyTrashed
* @return bool|null
*
* @throws PermissionRepositoryException
*/
public function read(int|string $id, $onlyTrashed = false)
{
try {

$this->model = $this->model->findOrFail($id);

} catch (\Throwable $exception) {

throw new ModelNotFoundException($exception->getMessage(), 0, $exception);
}

try {

return $this->model->deleteOrFail();

} catch (\Throwable $exception) {

throw new PermissionRepositoryException($exception->getMessage(), 0, $exception);
}

return null;
}

/**
* find and delete a entry from records
*
* @return bool|null
*
* @throws PermissionRepositoryException
*/
public function delete(int|string $id)
{
try {

$this->model = $this->model->findOrFail($id);

} catch (\Throwable $exception) {

throw new ModelNotFoundException($exception->getMessage(), 0, $exception);
}

try {

return $this->model->deleteOrFail();

} catch (\Throwable $exception) {

throw new PermissionRepositoryException($exception->getMessage(), 0, $exception);
}

return null;
}

/**
* find and restore a entry from records
*
* @return bool|null
*
* @throws PermissionRepositoryException
*/
public function restore(int|string $id)
{
if (! method_exists($this->model, 'restore')) {
throw new InvalidArgumentException('This model does not have `Illuminate\Database\Eloquent\SoftDeletes` trait to perform restoration.');
}

try {

$this->model = $this->model->onlyTrashed()->findOrFail($id);

} catch (\Throwable $exception) {

throw new ModelNotFoundException($exception->getMessage(), 0, $exception);
}

try {

return $this->model->deleteOrFail();

} catch (\Throwable $exception) {

throw new PermissionRepositoryException($exception->getMessage(), 0, $exception);
}

return null;
}
}
12 changes: 4 additions & 8 deletions src/Repositories/Eloquent/ProfileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,13 @@ public function list(array $filters = [])
*/
public function create(array $attributes = [])
{
try {
$this->model->fill($attributes);
if ($this->model->saveOrFail()) {
$this->model->fill($attributes);

$this->model->refresh();
if ($this->model->saveOrFail()) {

return $this->model;
}
} catch (\Throwable $e) {
$this->model->refresh();

throw new ProfileRepositoryException($e->getMessage(), 0, $e);
return $this->model;
}

return null;
Expand Down
12 changes: 4 additions & 8 deletions src/Repositories/Eloquent/RoleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@ public function list(array $filters = [])
*/
public function create(array $attributes = [])
{
try {
$this->model->fill($attributes);
if ($this->model->saveOrFail()) {
$this->model->fill($attributes);

$this->model->refresh();
if ($this->model->saveOrFail()) {

return $this->model;
}
} catch (Throwable $e) {
$this->model->refresh();

throw new RoleRepositoryException($e->getMessage(), 0, $e);
return $this->model;
}

return null;
Expand Down
13 changes: 5 additions & 8 deletions src/Repositories/Eloquent/TeamRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Fintech\Auth\Interfaces\TeamRepository as InterfacesTeamRepository;
use Fintech\Auth\Models\Team;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use InvalidArgumentException;
use Throwable;

Expand Down Expand Up @@ -56,17 +57,13 @@ public function list(array $filters = [])
*/
public function create(array $attributes = [])
{
try {
$this->model->fill($attributes);
if ($this->model->saveOrFail()) {
$this->model->fill($attributes);

$this->model->refresh();
if ($this->model->saveOrFail()) {

return $this->model;
}
} catch (Throwable $e) {
$this->model->refresh();

throw new TeamRepositoryException($e->getMessage(), 0, $e);
return $this->model;
}

return null;
Expand Down
14 changes: 4 additions & 10 deletions src/Repositories/Eloquent/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,13 @@ public function list(array $filters = [])
*/
public function create(array $attributes = [])
{
try {

$this->model->fill($attributes);
$this->model->fill($attributes);

if ($this->model->saveOrFail()) {
if ($this->model->saveOrFail()) {

$this->model->refresh();
$this->model->refresh();

return $this->model;
}
} catch (Throwable $e) {

throw new UserRepositoryException($e->getMessage(), 0, $e);
return $this->model;
}

return null;
Expand Down
14 changes: 5 additions & 9 deletions src/Repositories/Mongodb/PermissionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@ public function list(array $filters = [])
*/
public function create(array $attributes = [])
{
try {
$this->model->fill($attributes);
if ($this->model->saveOrFail()) {
$this->model->fill($attributes);

$this->model->refresh();
if ($this->model->saveOrFail()) {

return $this->model;
}
} catch (Throwable $e) {
$this->model->refresh();

throw new PermissionRepositoryException($e->getMessage(), 0, $e);
return $this->model;
}

return null;
Expand Down Expand Up @@ -113,7 +109,7 @@ public function update(int|string $id, array $attributes = [])
*
* @throws PermissionRepositoryException
*/
public function read(int|string $id, $onlyTrashed = false)
public function find(int|string $id, $onlyTrashed = false)
{
try {

Expand Down
12 changes: 4 additions & 8 deletions src/Repositories/Mongodb/ProfileRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,13 @@ public function list(array $filters = [])
*/
public function create(array $attributes = [])
{
try {
$this->model->fill($attributes);
if ($this->model->saveOrFail()) {
$this->model->fill($attributes);

$this->model->refresh();
if ($this->model->saveOrFail()) {

return $this->model;
}
} catch (Throwable $e) {
$this->model->refresh();

throw new ProfileRepositoryException($e->getMessage(), 0, $e);
return $this->model;
}

return null;
Expand Down
Loading

0 comments on commit f4d92fc

Please sign in to comment.