Laravel : Webhook class not found - Captain Hook - notifications

I'm trying to use captain-hook package to make webhook in my laravel app so that I can send a notification to a specific user when an eloquent event is fired.
I followed the package's documentation and put this code at the end of the method that will fire the event :
Webhook::create([
"url" => Input::get("url"),
"event" => "eloquent.saved: \App\DoctorRequest",
"tenant_id" => $user->id
]);
but I get this error :
FatalThrowableError in DoctorRequestController.php line 109:
Class 'App\Http\Controllers\Webhook' not found
How can I fix it ?

You haven't imported this in your controller.
Assuming you've added the Service Provider in your config/app.php file then you just need to import it at the top of your controller with:
use Webhook;
If you don't wish to import the Facade then you can reference it like this instead:
\Webhook::create([
"url" => Input::get("url"),
"event" => "eloquent.saved: \App\DoctorRequest",
"tenant_id" => $user->id
]);

Related

ASP.net core, AddSignInManager to get notification when a loggin happens

I am trying to get a notification for sign-in.
For this purpose, I created a custom SignInManager and overridden the method used to sign in .
Now if I do the following it works:
builder.Services.AddDefaultIdentity<IdentityUser>(options =>
{
// ...
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager<MySignMan<IdentityUser>>();
But in my code, I use AddIdentity instead of AddDefaultIdentity, and I don't find how to make it work without DefaultIdentity
Can this work for you?
builder.Services.AddIdentity<IdentityUser, IdentityRole>(option =>
{
option.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<ApplicationDbContext>().AddSignInManager<MySignManager<IdentityUser>>();

Yii2 Where to catch an exception thrown from a bootstrapper module?

In my config, I have a custom class bootstrapping to fetch data
'bootstrap' => [
[
'class' => 'app\components\DataProvider',
'campaignJsonUrl' => 'https://example.com/api.json',
],
],
And I would like to catch exceptions throwns for an unreachable url or invalid json for example. But I can't figure out where to catch these.
I made a custom error handler:
'errorHandler' => [
// use 'site/error' action to display errors
'class' => 'app\components\ErrorHandler',
'errorAction' => 'site/error',
],
In which the error seems to reach ErrorHandler.renderException() method if I override it, however it never reaches the SiteController.actionError() method, I guess because it happens before any controller action. How can I make the ErrorHandler run SiteController.actionError() so it will show the website's error page with the correct layout?
NOTE: My overridden renderException() method only sends the exception to a Slack API then returns parent::renderException($exception)

Laravel Multi-role unable to create in laravel 5

I am having trouble creating multi-role application in laravel5 since in laravel 5 the authentication is pre defined so I am not willing to mess around with predefined codes of laravel 5 authentication. I have a constructor that authenticates every controller in my project but I am unable to check user roles for the following roles:-
1. Admin
2. Agent
3. User
I can check manually for every functions but that is not the right process of doing so and if I have a total of around 500 functions I cant go in every function and define manually. please any help
Thank you
Personally I would use middleware and route groups to accomplish the task, which would be similar the way Laravel checks for user authentication.
You just have to determine when you need to run the middleware, which can be done by nesting Route::group's or injecting the middleware from your controller.
So, for an example of nesting you can have something like this in your routes file:
Route::group(['middleware' => ['auth']], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
return view('dashboard');
}]);
Route::group(['prefix' => 'company', 'namespace' => 'Company', 'middleware' => ['App\Http\Middleware\HasRole'], function () {
Route::get('dashboard', ['as'=>'dashboard', function () {
return view('company.dashboard');
}]);
Route::resource('employees', 'EmployeesController');
...
...
});
});
or you can inject the middleware to your controllers like so:
use Illuminate\Routing\Controller;
class AwesomeController extends Controller {
public function __construct()
{
$this->middleware('hasRole', ['only' => 'update'])
}
}
And then add a one or more Middleware files using something like php artisan make:middleware HasRole which will give you the middleware boiler plate which you could then add your role checking logic:
<?php namespace App\Http\Middleware;
use Closure;
class HasRole {
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if($request->is('admin/*')){
[******ADD YOUR LOGIC HERE TO DETERMINE THE ROLE ******]
[******YOU CAN ALSO INCLUDE ANY REDIRECTS IF NECESSARY******]
}
return $next($request);
}
}
Notice I used the $route->is('admin/*') to filter any routes as an example of further filtering requests, which you would probably not include if you are injecting the middleware from the controller.
But if the user passes the required role check you do not need to do anything and they will be allowed to continue to the view. If they fail the role check, you can handle that accordingly, but beware of getting them caught in a failed permission loop.
I assume you get the gist of it, feel free to look into the Laravel middleware docs for more info.

'auth' Middleware with Route::resource

How can I use middleware with resources?
Route::resource('myitem', ['middleware' => 'auth', 'uses' => 'App\\Controllers\\MyitemsController']);
Just followed https://laracasts.com/discuss/channels/general-discussion/struggling-with-routeresource-and-auth-middleware but unfortunately could not solve.
Getting error:
ErrorException (E_UNKNOWN)
Array to string conversion
Open: /vendor/laravel/framework/src/Illuminate/Routing/Router.php
protected function getResourceAction($resource, $controller, $method, $options)
{
$name = $this->getResourceName($resource, $method, $options);
return array('as' => $name, 'uses' => $controller.'#'.$method);
}
Using filter with resource was not working that why had to use Route::group
Route::group(array('before' => 'auth'), function()
{
Route::resource('myitem', 'App\\Controllers\\MyitemsController');
});
https://stackoverflow.com/a/17512478/540144
Middleware is a new feature of Laravel 5. In Laravel 4, filters where something similar. So instead of using the key middleware you should use before or after. Also, and that's where the error comes from, the second argument of Route::resource should be the controller name as string and the third one is an array of options:
Route::resource('myitem', 'App\\Controllers\\MyitemsController', ['before' => 'auth']);
Edit
Apparently before filters only work with resource routes when you wrap a group around it. See the OPs answer for an example...
I just came up against this and found the easiest way is to add the middleware straight to the controller.
I found my answer here:
http://laravel.com/docs/master/controllers
class MyitemsController extends Controller {
/**
* Instantiate a new MyitemsController instance.
*/
public function __construct()
{
$this->middleware('auth');
}
}
How to do this in Laravel 5. The Answer you have been waiting for.
Use middleware instead of before
Route::group(array('middleware' => 'auth'), function()
{
Route::resource('user', 'UserController',
['only' => ['edit']]);
}
To check if the route is setup, run:
php artisan route:list
which should show the following:
GET|HEAD | user/{user}/edit | user.edit | App\Http\Controllers\UserController#edit | auth
Note auth instead of guest
Better solution
Use middleware instead of before
Route::group(['middleware' => 'auth'], function(){
Route::resource('myitem', 'MyitemsController');
});
You can check if it's ok with:
php artisan route:list

phalcon currently dispatching route name

I use custom routes which include namespace besides controller and action. So for ACL purposes I use MVC route name as ACL resource name. Now I need to obtain currently DISPATCHING route name. The only solution I've come up with is to get namespace/controller/action from Dispatcher and iterating over all the routes find an appropriate one.
Is there any easiest way to obtain currently dispatching (not just matched) route name?
Pretty easy
\Phalcon\DI::getDefault()->get('router')->getMatchedRoute()->getName();
You can use your router, dispatcher and base controller to get what you need. Consider this:
$router = new \Phalcon\Mvc\Router(false);
$routes = array(
'/{namespace:"[a-zA-Z]+}/:controller' => array(
'controller' => 2,
),
'/{namespace:"[a-zA-Z]+}/:controller/:action/:params' => array(
'controller' => 2,
'action' => 3,
'params' => 4,
),
);
foreach($routes as $route => $params) {
$router->add($route, $params);
}
Now in your base controller you can do this:
public function getNamespace()
{
return $this->dispatcher->getParam('namespace');
}
This way you can have the namespace currently being served in your controllers (so long as they extend your base controller).
If you need to get the namespace in a model you can always use the DI like so (base model):
public function getNamespace()
{
$di = \Phalcon\DI::getDefault();
return $di->dispatcher->getParam('namespace');
}