Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add first app #44

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Modules/CircleApps/App/Http/Middleware/CheckAccountAppAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Modules\CircleApps\App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CheckAccountAppAccess
{

/**
* Handle an incoming request.
*/
public function handle(Request $request, Closure $next, string $app)
{
if(auth('accounts')->user() && (!has_app($app))) {
abort(403, 'Unauthorized');
}

return $next($request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Modules\CircleApps\App\Http\Middleware\CheckAccountAppAccess;
use Modules\TomatoCategory\App\Facades\TomatoCategory;
use Modules\TomatoCategory\App\Models\Category;

Expand Down Expand Up @@ -43,6 +44,8 @@ public function boot(): void
$this->app->bind('circle-apps-menu', function () {
return new \Modules\CircleApps\App\Services\CircleAppsMenuServices();
});

$this->app['router']->aliasMiddleware('app', CheckAccountAppAccess::class);
}

public function registerComponents(): void
Expand Down
13 changes: 11 additions & 2 deletions Modules/CircleApps/App/Providers/helpers.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
<?php

if(!function_exists('has_app')) {
function has_app(string $key): bool
function has_app(string $key, ?int $account=null): bool
{
if(auth('accounts')->user()){
$hasApp = auth('accounts')->user()->apps()->where('key', $key)->first();
if($hasApp){
return true;
if($account){
if($account == auth('accounts')->user()->id){
return true;
}
else {
return false;
}
}

return true;
}
}

Expand Down
1 change: 1 addition & 0 deletions Modules/CircleApps/resources/views/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,6 @@
</div>
</div>
</div>
</div>
</div>
</x-circle-xo-app-layout>
18 changes: 9 additions & 9 deletions Modules/CircleApps/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
|
*/

//Route::middleware(['web', 'splade'])->prefix('apps')->name('apps.')->group(function () {
// Route::get('/', [CircleAppsController::class, 'index'])->name('index');
// Route::get('/{app}', [CircleAppsController::class, 'show'])->name('show');
//});
//
//Route::middleware(['web', 'splade', 'auth:accounts'])->prefix('apps')->name('apps.')->group(function () {
// Route::post('/{app}/install', [CircleAppsController::class, 'install'])->name('install');
// Route::post('/{app}/uninstall', [CircleAppsController::class, 'uninstall'])->name('uninstall');
//});
Route::middleware(['web', 'splade'])->prefix('apps')->name('apps.')->group(function () {
Route::get('/', [CircleAppsController::class, 'index'])->name('index');
Route::get('/{app}', [CircleAppsController::class, 'show'])->name('show');
});

Route::middleware(['web', 'splade', 'auth:accounts'])->prefix('apps')->name('apps.')->group(function () {
Route::post('/{app}/install', [CircleAppsController::class, 'install'])->name('install');
Route::post('/{app}/uninstall', [CircleAppsController::class, 'uninstall'])->name('uninstall');
});

Route::middleware(['auth', 'splade', 'verified'])->name('admin.')->group(function () {
Route::get('admin/apps', [\Modules\CircleApps\App\Http\Controllers\AppController::class, 'index'])->name('apps.index');
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Modules\CircleContacts\App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class CircleContactsController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return view('circlecontacts::index');
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('circlecontacts::create');
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
{
//
}

/**
* Show the specified resource.
*/
public function show($id)
{
return view('circlecontacts::show');
}

/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
return view('circlecontacts::edit');
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id): RedirectResponse
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
//
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<?php

namespace Modules\CircleContacts\App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Modules\CircleContacts\App\Http\Requests\CircleXoContact\CircleXoContactStoreRequest;
use Modules\CircleContacts\App\Http\Requests\CircleXoContact\CircleXoContactUpdateRequest;
use Modules\CircleContacts\App\Models\CircleXoContact;
use TomatoPHP\TomatoAdmin\Facade\Tomato;

class CircleXoContactController extends Controller
{
public string $model;

public function __construct()
{
$this->model = \Modules\CircleContacts\App\Models\CircleXoContact::class;
}

/**
* @param Request $request
* @return View|JsonResponse
*/
public function index(Request $request): View|JsonResponse
{
$query = CircleXoContact::query();
$query->where('account_id', auth('accounts')->user()->id);
if($request->has('group_id')){
$query->whereHas('groups', function ($query) use ($request){
$query->where('id', $request->get('group_id'));
});
}

return Tomato::index(
request: $request,
model: $this->model,
view: 'circle-contacts::contacts.index',
table: \Modules\CircleContacts\App\Tables\CircleXoContactTable::class,
query: $query
);
}

/**
* @param Request $request
* @return JsonResponse
*/
public function api(Request $request): JsonResponse
{
$query = CircleXoContact::query();
$query->where('account_id', auth('accounts')->user()->id);
return Tomato::json(
request: $request,
model: \Modules\CircleContacts\App\Models\CircleXoContact::class,
query: $query
);
}

/**
* @return View
*/
public function create(): View
{
return Tomato::create(
view: 'circle-contacts::contacts.create',
);
}

/**
* @param CircleXoContactStoreRequest $request
* @return RedirectResponse|JsonResponse
*/
public function store(CircleXoContactStoreRequest $request): RedirectResponse|JsonResponse
{
$request->merge([
"account_id" => auth('accounts')->user()->id
]);
$response = Tomato::store(
request: $request,
model: \Modules\CircleContacts\App\Models\CircleXoContact::class,
message: __('Contact saved successfully'),
redirect: 'profile.contacts.index',
hasMedia: true,
collection: [
'avatar' => false,
]
);

$response->record->groups()->sync($request->get('groups'));

if($response instanceof JsonResponse){
return $response;
}

return back();
}

/**
* @param \Modules\CircleContacts\App\Models\CircleXoContact $model
* @return View|JsonResponse
*/
public function show(\Modules\CircleContacts\App\Models\CircleXoContact $model): View|JsonResponse
{
if(!has_app('circle-contacts', $model->account_id)){
abort(403);
}

return Tomato::get(
model: $model,
view: 'circle-contacts::contacts.show',
hasMedia: true,
collection: [
'avatar' => false,
]
);
}

/**
* @param \Modules\CircleContacts\App\Models\CircleXoContact $model
* @return View
*/
public function edit(\Modules\CircleContacts\App\Models\CircleXoContact $model): View
{
if(!has_app('circle-contacts', $model->account_id)){
abort(403);
}

$model->groups = $model->groups->pluck('id')->toArray();
return Tomato::get(
model: $model,
view: 'circle-contacts::contacts.edit',
hasMedia: true,
collection: [
'avatar' => false,
]
);
}

/**
* @param CircleXoContactUpdateRequest $request
* @param \Modules\CircleContacts\App\Models\CircleXoContact $model
* @return RedirectResponse|JsonResponse
*/
public function update(CircleXoContactUpdateRequest $request, \Modules\CircleContacts\App\Models\CircleXoContact $model): RedirectResponse|JsonResponse
{
$response = Tomato::update(
request: $request,
model: $model,
message: __('Contact updated successfully'),
redirect: 'profile.contacts.index',
hasMedia: true,
collection: [
'avatar' => false,
]
);

$response->record->groups()->sync($request->get('groups'));

if($response instanceof JsonResponse){
return $response;
}

return back();
}

/**
* @param \Modules\CircleContacts\App\Models\CircleXoContact $model
* @return RedirectResponse|JsonResponse
*/
public function destroy(\Modules\CircleContacts\App\Models\CircleXoContact $model): RedirectResponse|JsonResponse
{
if(!has_app('circle-contacts', $model->account_id)){
abort(403);
}

$model->groups()->detach();
$model->contactMeta()->delete();
$response = Tomato::destroy(
model: $model,
message: __('Contact deleted successfully'),
redirect: 'profile.contacts.index',
hasMedia: true,
collection: [
'avatar' => false,
]
);

if($response instanceof JsonResponse){
return $response;
}

return redirect()->route('profile.contacts.index');
}
}
Loading
Loading