How to inject container in FormType class in Symfony2/3 - fosuserbundle

I am overriding FOSUserBundle group form, and I want to add a select to choose a role form thom available in security.yml.
I would like if possible to avoid to override the controller as well.
so in my FormType I have
$roleHierarchy = $this->container->getParameter('security.role_hierarchy.roles');
but $this->container is not available. Can I inject it in the controller from service.yml?
thanks

Related

ag-grid + angularJS header and cells editable by the user

I just start using the ag-grid library with angularJS to display some tabular data in my application. The thing now is that I want the user to be able to alter the header value of each column like as he/she can edit the cells within a column when we enable the editable attribute. While searching for a solution I realized that there is no default way to turn the header into an editable element. So I was wondering which are my alternatives.
One thought of mine was to add to each header an edit button and when clicked a modal window to pop up with the corresponding input fields of the header and its column's cell values. In this case, the editable attribute is not going to be used because the edit of the cells will be conducted within the modal. Something like that but instead for the row, the button should be in each header.
So which are the ways that I can achieve something like that? I read about header template but couldn't really found any straightforward way to approach it.
Any ideas/hints or other alternatives are welcome.
Make a service which have an array as DataSet of your grid, and provide it with handlers for the events of a CRUD in the same service. Inject the service into the controller of your modal and view of the grid. After that, call the object gridDataSetadd,remove,update who is in the service, then $digest do the rest of the work once DataSet will be modified.
UPDATE, answering the comment
Inject your service into your controller:
For .ts
static $inject = ['$scope','YourService'];
constructor($scope, userService: YourService) {
this.userService.getSomething();
}
For .js
.controller('YourCtrl', ['$scope', 'YourService', function ($scope, YourService) {
$scope.someVar = YourService.someVar;
}])

How to access cakephp model's validate from custom view template

I have created a 'form' template to bake custom theme views (Cake 2.6.0). I am trying to access field properties from the Model's $validate array. However, accessing $model->validate shows an empty array. My model has several fields with rules defined in it's $validate property.
Is the $validate property not accessible while baking custom views? If not, how do I find out whether a field is required, or if it uses 'rule =>' 'url', for example?
The view template(s) used by cake bake view are an instance of class TemplateTask and does not have direct access to the Model, View or Controller. What you want to do is import the controller to your custom view template:
Console\Templates\[themename]\views\[template].ctp
<?php
// The Controller's name
$controllerName = Inflector::pluralize($modelClass).'Controller';
// Import the Controller
App::import('Controller', $controllerName);
// Instantiate the Controller
$Controller = new $controllerName();
// Load the Controller's classes
$Controller->constructClasses();
//...the rest of your template
You now have access to your controller # $Controller. To access your validate property, you would use $Controller->{$modelClass}->validate.

temporarily disable controller redirect

I'm trying to re-use a plugin's controller method for adding a user (the plugin is usermgmt, a purchased plugin). The plugin's method does everything I need, however it finishes by doing a redirect to another page.
I don't want to modify the plugin code, and I am wondering if it is possible to temporarily disable the Controller:redirect method
class MyController extends AppController{
function signUpUser(){
//populate $this->request->data to be what plugin controller method is looking for
//temporarily disable Controller::redirect
//make call to plugin's controller method
$usersController = new UsersController();
$usersController->constructClasses();
$usersController->addUser();
//re-enable Controller::redirect
//Do my own redirect
$this->redirect('/welcome');
}
}
You are not supposed to use controllers that way, there's probably a model or component which you could use instead. Extending the users controller may also be an option. Anyways, since you said you've bought it, why don't you contact the support, they should know best how their stuff works?
That being said, let me answer the actual question. In order to be able to disable redirection via your controller, you would have to override the Controller::redirect() method and implement some logic that allows you to disable the functionality.
Here's an untested example, it uses a property which defines whether redirects are enabled:
public $redirectEnabled = true;
public function redirect($url, $status = null, $exit = true) {
if($this->redirectEnabled) {
parent::redirect($url, $status, $exit);
}
}
This should be pretty much self-explantory.
In your controller you could set $this->redirectEnabled to true/false to enable/disable the redirect functionality.

Prefix route I want to create already exists as controllers

I'm new to cakephp so go easy on me!
I have a table called promoters and users can be promoters.
I want to create a "promoters dashboard/control panel". To do this I wanted to create a prefix route of "promoters".
E.g. you go to http://mywebsite.com/promoters/events/add you get to the promoter_add function within EventsController.
So the problem is that I actually have a controller called PromotersController which I need for the admin users (e.g. http://mywebsite.com/admin/promoters/add).
I hope you're following.
So... my question is, will I just have to do what I think I'm going to have to do which is to create a different prefix route? Maybe call it promos or something?
Or can I create a prefix route of promoters and just have it override the controller named PromotersController if it needs to?
Thanks
Can't you just add the promoters dashboard method to the PromotersController?
class PromotersController extends AppController {
public function dashboard() {
}
}
you can then visit:
/promoters/dashboard

Cakephp: Routing and default views

I'm currently trying to add an admin mode to my cakephp site.
I followed a tutorial on the web and added a routing prefix:
Configure::write('Routing.prefixes', array('admin'));
Then I implemented login and logout functionality.
I added a admin_view.ctp and admin_index.ctp to a model where I want to restrict access. Therefore I deleted view.ctp and index.ctp and expected that only admins could view the model by using:
http://xxxx/model/admin/index
But when I entered
http://xxxx/model/index
a default view appeared that I could not disable (it allows model manipulation). Is there a standard way to disable all these default views or do I have to create a index.ctp and view.ctp that simply show error messages?
If you want to disallow certain action, you need to setup ACL, which is described here. And for authentication, in your controller you need something like this:
class SomethingsController extends AppController
{
var $components = array('Auth');
//this will allow all users access to the index
//and view methods ( but not any other)
function beforeFilter() {
$this->Auth->allow('index','view');
}
// other actions
}
Hope this helps.
Delete default controller methods: function index(), view(), add(), edit() and delete(). Removing *.ctp templates is not enough.

Resources