Laravel : Should I extend my User class to 'Authenticatable'? - angularjs

Im building an app with AngularJS and Laravel. I'm currently working on a login page.
By default in Laravel, the 'out of the box' class User extends 'Authenticatable' but I would like to know if I can rather simply extend it to 'Model', because I actually want to rule my authentication access through AngularJS routing and not via Laravel. I don't know if this makes sense.. and if this would even work (I dont wanna break the logic behind Laravel). Could anyone help me on that?

The Authenticatable refers to Illuminate\Foundation\Auth\User which in turn includes some Traits and implements some contracts, depending on whether you're going to use Laravel's Auth system it seems like you can omit extending this class without any problems.

Related

Cakephp3 handle authorization as a plugin

I've been working on cakephp3 for a while now. I've always used Cakephp's Auth component for authorization and authentication purpose.
I follow the very conventional procedure every time, like loading the component, adding isAuthorized function in controllers and defining allowMethods etc.
But now what I want is to develop my own plugin for this purpose, just using Cake's Auth component. So that i can reuse the plugin in all my future projects, also i want it to be like plug and play. Like You enable it, add few settings and your User management is done.
I know that how migrations work so I can add users table via migration every time. (Just an idea)
The thing I don't get right now is how to make everything separate from the core app? Like everything is done via plugin and nothing is added to every controller of the app.
Hope I'm clear about what I want to achieve.
Update: I know there is a whole list of third party Auth plugins. But I want to develop my own so i just need the idea of how things work.
Any solutions to my problem would save my day.

CakePHP bake generating auth component user actions

I'm sure there is a relatively simple answer. I am trying to use CakePHP 2.0 to bake an application from the command line, and i would like cake to create the basic AuthComponent methods for my user model. the database table for users is named users, which includes the two necessary fields for auth, username and password.
I've been able to include AuthComponent in my usersController from bake, but have been unsuccessful at getting it to generate basic usersController actions for auth, such as login() and logout().
How can I do this? generating this skeleton code will help save a lot of time.
Thanks in advance!!!!
default bake templates are only for CRUD (create=add, read=index,view update=edit, delete=delete), not for the rest. but it is fairly simple to customize your templates according to your needs.
Although login/logout are one-time-needed methods and don't make any sense to be baked. Its easier and faster to just manually "make" them. For login use the add template as basis, logout doesnt need any view.

Best way to implement admin panel in CakePHP

I am trying to move from CodeIgniter to CakePHP and can't figure out the best way to implement an admin panel. In CI I would create two different applications, one for the frontend and one for the admin panel.
After Googling around, I have found three ways to implement admin panel in CakePHP:
Routing - I don't want to use this as I want by Controllers/Models to be separate for frontend and admin panel
Plugin
Two separate apps
Should I use plugin to implement admin panel or should I have separate apps? Any benefits of one over the other?
I normally develop the admin/backend as a plugin. This keeps your backend/admin controllers/views/models separated from the frontend and you don't have to jump through hoops to have separate stylesheets, layouts etc.
Another advantage is that both front- and backend are still part of the same application, so if desired, you can share logic/components, for example you'll be able to put helpers that are usable both for front- and backend in another plugin (e.g. plugins/Shared or plugins/Handytexttools) and use those both wherever you want
As a rule of thumb; put components that may be reuseable for other projects in a separate plugin, this way you can just add those plugins to other projects without problems. Keep your plugins simple; it's no problem to create a plugin containing just one or two helpers or models and a few files of JavaScript. This will make it easier to 'cherry pick' the plugins that you need for a project. Once Cake has 'cached' the file-locations of all classes in your plugins, the overhead of separate plugins should be minimal.
Coming back to the 'admin' plugin. Try to only include code specific for this project in your admin plugin and reusable parts in another one (e.g. Generic stylesheets and layouts for admin-panels). You'll be able to start a admin-plugin for your next project with minimal coding
Good luck with your project and enjoy CakePHP
If you want to keep your controllers and models separate - I'd go with a separate app, although you'll end up with a bunch of duplicate code between the apps (maintenance headache waiting to happen).
My choice would be admin routing and an admin theme.
Enable admin routing in /app/Config/core.php
In AppController beforeFilter():
$this->theme = isset($this->params['admin']) ? "Admin" : "Site";
Move all your site views and assets into /app/View/Themed/Site/
Create your admin themes in /app/View/Themed/Admin
Old and refers to CakePHP 1.3, but still is a question you should check: CakePHP admin panel
The Cake way is routing. I'd go with a plugin like CakeDC Users that makes things easier.
You could use admin-routing. Check out:
http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing
Another solution -which I find really easy to implement- is like this:
In your AppController:
public function beforeFilter(){
$this->set('current_user', $this->Auth->user());
}
This makes the $current_user available in your app.
Then in your view-files, you can check:
<?php if ($current_user['role'] == 'admin'){/*place code for admin users to see here*/} ?>
<?php if ($current_user){/*place code for logged-in users to see here*/} ?>
I know this is an old thread. But would like to ask if anyone had trouble implementing the admin panel as a plugin. Particularly duplication of code.
For example you're implementing an e-commerce site. You have an OrderController both in the main and admin plugin. Don't you think it's kinda hard to maintain the logic in two places?
How about just using one main controller. It's serves two purpose. One as an API then the controller for your Admin webapp.
Your public side would then basically communicate via API to fetch data.
Do you think it's a good idea?
You can use admin views like admin_index.ctp just change this
//Configure::write('Routing.admin', 'admin');
to
Configure::write('Routing.admin', 'admin');
in core.php and in the controller add admin_index() function

Best Practice for Adminpanel with Acl?

I´m just working with Cakephp for a few days and I´m very impressed. But now I´m trying to get closer with Acl, but it´s a bit confusing.
My situation is, that I want to create a website with a frontend and a backend. But I´m not sure if I really need Acl for this, cause all Pages should be available for all users, except the backend of course. The tutorials in the Cookbook aren´t very helpful due to the fact, that it´s all about creating users, and groups and roles and creating the right views for login, adding and editing users, etc.
But I just need information about what Acl handles? Does it restrict the use of controllers or models?
Or do I need something else than Acl? Maybe it´s easier to check a session variable and redirect direct into the controller if the check false?
Hopefully you can bring me on the right way,
thanks in advance and best greetings from Germany,
Sascha
I suggest you to read this chapter and use the Auth component instead of simply accessing the session as you're teased to do.
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
For your admin backend use prefix routing.
http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing
In conjunction with auth this is pretty easy to check and implement in the isAuthorized() callback.
If you don't need various 'levels' of permissions; i.e. any logged-in user is allowed to access the backend, it's best to skip ACL (for now). If, in a later stage, ACL is required, you can always add it later.
You can start with 'simple' authentication. This chapter in the cookbook describes how to do so;
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
In general, do not develop features that you don't need now. E.g. implementing ACL because you might need it in the future is only overcomplicating your development and chances are, they don't fit the requirements when that moment arrives.
Unlike #burzum, I'm not a big fan of prefix routing (only for very simple projects), because you'll end up mixing front- and backend actions and logic in the same controller.
I would advice to create separate controllers for the backend, possibly develop them as a Plugin. Either way, you may consider to create 'base' Controllers and Models for the backend and have your backendcontrollers/models extend them. This way you'll be able to define components/behaviors to use for the backend in 1 location. Also, by loading the 'Auth' component only in Backend controllers, you don't have to 'allow' actions in each controller in the frontend
for example;
class BackendCoreController extends AppController {
// only load the Auth component in backend controllers
// regular/frontend controllers don't require authentication
$components = array('Auth');
}
class PageAdminController extends BackendCoreController {
}
For considerations on developing the backend as a plugin, see my answer here:
Best way to implement admin panel in CakePHP

CakePHP: Overrride Controller method with plugin

I'm new to this framework and looking to extend/override a Controller method with a plugin. If this is the wrong way to do this please let me know. I just want the solution to be modular in that there is no tampering with the extended controller. This is what I'd like..
MyController extends AppController{
function index(){}
}
I have a plugin
MyPlugin extends MyController{
function index(){}
}
I want when MyController->index() gets called it runs MyPlugin->index(). Any help would be appreciated.
What you're asking for is some kind of "reverse-inheritance", which is not supported in PHP (one might consider Aspects in Java or ObjectiveC's categories).
As far as I understand, you have the following situation:
"3rd Party App's Controller class" extends "CakePHP's Controller class"
You get both classes from external upstreams and want to modify the 3rd Party App's Controller without sacrificing either upstream. This is not possible by language, and as far as I can tell CakePHP offers no framework solution.
You might be able to achieve your goal by modifieng the routing, i.e. redirect requests to MyController to your custom MyPlugin controller and call MyController's action from there.

Resources