Why dynamic scaffolding with Grails doesn't work proprely? - grails

I've tried to scaffold dynamically some domains in GRAILS , but after adding items into database they doesn't show up in the listing view (User/index) in this case :
User.groovy
package scaffold_test
class User {
String username
String address
static constraints = {
}
}
UserController.groovy
package scaffold_test
class UserController {
def scaffold=true
def index() { }
}
so after running the application, I can see the User controller in the Grails home page,I can add Users, and I can see the added instances via DbConsole. But i can't list the instances in the User/index view. Thanks !

Delete def index() { } to let the scaffolding generate all of the methods.

Related

How create a listener to a attribute service in grails

I have a problem. I am programming a grails application and I want create a listener to determinate attribute on my service class that I use it on my controller. When this attribute changes I want that the controller be notified. Someone can give a tip or the way to do this? The pseudo-code controller and service are bellow.
class myController{
def someService
def index() {
def return = someService.doSomething();
render (template: "template")
}
}
My Service
class myService {
private void doSomething(){
//some code here changing the variable to be listened
...
...
}
}

groovy.lang.MissingPropertyException

I have been trying to use dynamic scaffolding in groovy on grails to see the list of the Tasks. My domain class is as follows
package projecttracker
import java.util.Date;
class Task
{
String name
String description
Date dueDate
String toString() {
"${name}"
}
static belongsTo = [enduser: EndUser ,project: Project]
static constraints = {
name()
description()
dueDate()
}
}
My controller class is as follows:
package projecttracker
class TaskController {
def scaffold =true
def index() {
redirect(action:list)
}
}
But, whenever I call the index method of the Task controller, the following error occurs:
URI : /ProjectTracker/task/index
Class: groovy.lang.MissingPropertyException
Message: No such property: list for class: projecttracker.TaskController Possible solutions: edit, flash, class
Around line 7 of grails-app\controllers\projecttracker\TaskController.groovy
4: def scaffold =true
5:
6: def index() {
7: redirect(action:list)
8: }
9:}
Can someone tell me, how to define the list method here? Thanks in advance.
Make sure you use a string for the method name, and the scaffold variable must be static, i.e.
static scaffold = true
def index() {
redirect(action:'list')
}

Grails Class with hasMany

I have this domain class that has a one-to-many relationship as with dynamic scaffolding show below:
Domain:
package mienapp
class Announcements {
String user
String title
String comments
Date dateCreated
static hasMany = [tag: Tags]
static mapping = {
comments sqlType: 'text'
}
static constraints = {
}
}
Controller:
package mienapp
class AnnouncementsController {
def scaffold = true
def index() {
redirect(action: list)
}
}
When controller redirects to list, the table shows all fields defined in announcements class. How can I show the value of field from tags in the table as well?
Assuming your list method returns a model with an Announcements instance as
def list() {
..
[announcementsInstance: announcementsInstance, ...]
}
in your view, you can access tags like so
<g:each in="${announcementsInstance.tag}" var="tag">
${tag.someproperty}
</g:each>

How to Configure module in Admin Panel in Orchard Cms

I am new to orchard i had made a simple module using MVC and Entity Frame work ..The module is simple Crud application.I had integerated this module on my orchard site and this module works fine on front end.But i had problem how to configure same module on admin panel in orchard i want to do these functionalities on admin site
Create a controller called AdminController or decorate a controller with the [Admin] attribute (using Orchard.UI.Admin).
Then to create a menu item create a file called AdminMenu.cs in the route of your project with the following code:
using Orchard.Localization;
using Orchard.UI.Navigation;
namespace MyFirstModule {
public class AdminMenu : INavigationProvider {
public Localizer T { get; set; }
public string MenuName { get { return "admin"; } }
public void GetNavigation(NavigationBuilder builder) {
builder.Add(T("My admin menu item"), "50",
menu => menu.Add(T("My admin menu item"), "20", item => item.Action("Index", "Admin", new { area = "MyFirstModule" })
.Permission(Permissions.ConfigureRobotsTextFile)));
}
}
}
This will add a menu item that links to your action. Then you can do whatever admin stuff you want :)

Configure authorized roles dynamically via a config file in MVC Application

I current have the following attribute decorating one of the action method.
[Authorize(Roles = "Admin")]
public ActionResult DoAdminTask()
{
//Do something
return View();
}
Currently, only users in the Admin role can invoke this method, but this will change. Is there anyway I can store a list of authorised roles in a config file, rather than hard coding it into the source?
EDIT: Roles will change over time, and more than 1 role will need access.
i.e. Users in either role A OR role B can access.
No way to do this with the standard authorize attribute, but you could extend the authorize attribute with your own custom authorize attribute and have it use a configuration file to determine the mapping between controller/action and the set of roles.
but you can use something like
public static class AppRoles
{
public const string Users = "UsersRoleName";
public const string Admin = "AdminRoleName";
}
and then Controller can have authorize attribute as
[Authorize(Roles = AppRoles.Admin)]
I felt this question deserved an answer with a code sample... Taking #tvanfosson's suggestion of extending the AuthorizeAttribute class, here's what I came up with (criticism is more than welcome).
AuthorizeFromConfiguration.cs:
public class AuthorizeFromConfiguration: AuthorizeAttribute
{
public new string Roles
{
get {
return base.Roles;
}
set {
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("authorization.json")
.Build();
base.Roles = config[value];
}
}
}
authorization.json:
{
"Parts": {
"Create": "contoso.com\\MyWebApp_CreateNewPart",
"Edit": "contoso.com\\MyWebApp_EditPart"
}
}
Example Usage:
[AuthorizeFromConfiguration(Roles = "Parts:Create")]
public class CreateModel : PageModel
{
//...
}
Note: In my testing, the web-site had to be restarted before any changes to authorization.json file took effect, even when I tried changing the logic so that the JSON file was read on the get accessor instead of the set.

Resources