Laravel: Set Auth config for Package - authentication

I am creating my own package and I want to add Auth only for my package using a different table that the app auth table.
I can't found the way to override the app auth.table config only for my package.
Searching I found this solution, that change the config on the fly.
In my code:
class EasytranslateServiceProvider extends ServiceProvider {
[...]
/**
* Bootstrap the application events.
*
* #return void
*/
public function boot()
{
$this->package('revolistic/easytranslate');
// add the packages routes
include __DIR__.'/../../routes.php';
// doesn't work
$this->app['config']['auth'] = \Config::get('easytranslate::auth');
}
[...]
}
But it doesn't work, look like the Auth module is reading the configuration before the package creation or boot() function call.
If I do:
class EasytranslateServiceProvider extends ServiceProvider {
[...]
/**
* Bootstrap the application events.
*
* #return void
*/
public function boot()
{
$this->package('revolistic/easytranslate');
// add the packages routes
include __DIR__.'/../../routes.php';
// doesn't work
$this->app['config']['auth'] = \Config::get('easytranslate::auth');
// show that the changes was made
print_r($this->app['config']['auth']);
}
[...]
}
I get that the config was changed, but the Auth model is still taking the table name from the app auth config file.
I am using the last version of Laravel, any idea how I can accomplish it?
Thanks in advance

Oh, this solution works i found my error i should to namespace my model in my package auth file like that
'model' => 'Revolistic\Easytranslate\User',
Cheers

Related

Laravel 5.2 How to change redirects of RedirectIfAuthenticated depending on Controller?

I'm wondering if it is possible to make the authentication redirect differently for each of my controllers? Currently, everything redirects to /home. This is intended for my HomeController. But for ClientController, I want it to redirect to /client (if authenticated) and not /home. Do I have to make a new middleware for each of my controllers or is there a way to accomplish this by reusing auth?
RedirectIfAuthenticated.php
if (Auth::guard($guard)->check()) {
return redirect('/home'); //anyway to change this to /client if coming from ClientController?
}
I have this on my ClientController.php
public function __construct()
{
$this->middleware('auth');
}
Thanks in advance! Fairly new to Laravel and Middleware.
Just use this in User model:
protected $redirectTo = '/client';
You can also achieve this by changing Laravel's core file. If you are using Laravel 5.2 go to project_folder\vendor\laravel\framework\src\Illuminate\Foundation\Auth\RedirectsUsers.php
You can find the following code:
public function redirectPath()
{
if (property_exists($this, 'redirectPath')) {
return $this->redirectPath;
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home'; //Change the route in this line
}
Now, change /home to /client. However, I recommend not to change core files. You can use the first one.
Never mind, I was able to make things work with proper routing.
Added ClientController under web middle which is responsible for all of the authentication.
Route::group(['middleware' => ['web']], function () {
Route::resource('client', 'ClientController');
}
And in
ClientController.php, add to use auth middleware.
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('client');
}

express: project-local middleware extending Express.Request

I am working on a node server based on express and written in TypeScript 1.7. I'm using some project-specific middlewares, that extend the existing express Request or Response interface, but I can't get it completely working yet (without tsc complaining about not finding X in req or res). I've found other questions about this issue, but they are either out of date (I guess) or the solution is not straight forward.
I took a look at the definition of an existing middleware, but I didn't get it working without manually writing separate d.ts files and referencing them in typings/tsd.d.ts. This is my setup:
// middleware/foobar.ts
declare module Express {
export interface Request {
foobar?: string;
}
}
/* my project-related middleware extends the request by `foobar` */
export = function(req: Express.Request, res, next) {
req.foobar = 'FooBar';
next();
};
// main.ts
import express = require('express');
var app = express();
app.use(require('./middleware/foobar'));
app.get('/foobar', (req, res) => {
/* tsc: Property 'foobar' does not exist on type 'Request' */
res.send(req.foobar);
});
What's the best practice for extending express' Request and Response interfaces? If possible, without the need of writing a separated d.ts, manipulating anything within the typings directory or using /// <reference path="..." /> comments.
I've found a solution myself, using this example as a reference. My middleware needs to be wrapped in a declared module, so middleware/foobar.ts looks like this:
declare module Express {
export interface Request {
foobar?: string;
}
}
declare module 'foobar' {
function foobar(req: Express.Request, res, next) {
req.foobar = 'FooBar';
next();
}
export = foobar;
}
It's even a bit more trickier if you are using classes or other imported stuff within your middleware. In my example, my middleware uses my own "EntityManager" class, which is an abstraction to the database connection (mysql for me). My middleware (it's middleware/database.ts for me) looks like this now:
declare module Express {
import { Manager as EntityManager } from 'entity-manager';
export interface Request {
entityManager?: EntityManager;
}
}
declare module 'database' {
import * as mysql from 'mysql';
import { Manager as EntityManager } from 'entity-manager';
/* some middleware-related code */
var pool = mysql.createPool(...);
var entityManager = new EntityManager(pool);
/* *** */
/* the actual exported function */
function database(req: Express.Request, res, next) {
req.entityManager = entityManager;
next();
};
export = database;
}
Note that my EntityManager class is imported twice, once per module declaration. It did not seem to work by just importing it above both modules.
UPDATE
Having the actual code in a declared module ('database' in my case) produces no output in the JS file.
Having that code within a regular module requires the name not being in apostrophes (i.e. hyphens would not be allowed there for example) and doesn't produce a one-function-export code either.
Having the actual code completely out of a module (so there's only the declared Express module with the extended Request) produces correct JS but my text editor can't find entityManager in req anymore.
It seems like I'm needed to put the typings (my own extensions to Express.Request for example) into a dedicated d.ts file, where no actual code is present.

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.

communicating between modules in zend framework 2

I have an authentication module. Now, I want to ensure that every module passes (communicates) with that authentication module. I guess you could say its the authentication to the entire application. How do I accomplish this?
Well one simple way would be getting that module/module_class via namespaces, then you could just extend the class. Have the functionality automatically called in the parent class or call the method in the child class. This would be a pretty basic way:
// Auth class
class SomeAuthClass
{
public function __construct()
{
// go ahead and call doAuthCrap here, or wait
// and let the child class call it manually
}
protected function doAuthCrap()
{
// code
}
}
use Your\AuthModule\SomeAuthClass;
class SomeOtherModuleClass extends SomeAuthClass
{
public function zippy_snipp()
{
// call some method from the parent auth class (doAuthCrap)
}
}
Or to adhere to some of the new ways ZF2 does things, you could access the auth class via the service manager and write the config for it in service config in your module.php file. There's really multiple ways to go about doing this one and ZF2 offers quite a bit of options for doing stuff like this.
zf2:
// in controller
$auth = $this->getServiceLocator()->get('someAuth');
// in service config in module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'someAuth' => function ($serviceManager) {
// code here
},
)
);
}

How to set CRUD in a subpackage with Play

I am trying to use the CRUD module in subpackages instead of the default one.
package controllers.admin;
import models.Branch;
import controllers.CRUD;
#CRUD.For(Branch.class)
public class Branches extends CRUD {
public static void index() {
render();
}
}
My routes file is:
# Import CRUD routes
* /admin module:crud
However when I use the default url: http://localhost:9000/admin/
I get the template not found error:
Template not found
The template admin/Branches/index.html does not exist.
How can I specify to CRUD module to look for views in the subpackages?
ok. controller was generated with eclipse plugin and included the index() method.
Removing it solved the problem and the module is now working correctly.