Announcement Announcement Module
Collapse
No announcement yet.
How to insert current username in to table and filter views. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to insert current username in to table and filter views.

    Hi, How do I fill in the current username in to a hidden field so I can limit access to those records?

    I've been searching for a while and I found this code that should grab the username. I found no specific answer so I'm asking here.

    Code:
    @RequestMapping(method = RequestMethod.GET)   
     public ModelAndView showResults(final HttpServletRequest request, Principal principal) {
            final String currentUser = principal.getName();
    currently I'm using a drop down list to select the sales person.

    my src/domain/SalesOrder.java:
    Code:
    //TODO: Auto-populate Sales Tech field with user id    
        @NotNull
        @ManyToOne
        private SalesTechnician salesTech;
    My src/web/SalesOrderController.

    Code:
    import com.aaceglass.domain.SalesOrder;
    import org.springframework.roo.addon.web.mvc.controller.finder.RooWebFinder;
    import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RequestMapping("/salesorders")
    @Controller
    @RooWebScaffold(path = "salesorders", formBackingObject = SalesOrder.class, delete = false, update = false)
    @RooWebFinder
    public class SalesOrderController {
    }
    and the relevant line from create.jspx
    Code:
    <!-- TODO: Change this field to Hidden, and insert logged in user name -->
    <field:select field="salesTech" id="c_com_aaceglass_domain_SalesOrder_salesTech" itemValue="id" items="${salestechnicians}" path="/salestechnicians" required="true" z="5NC$

    Then I have ROLE_SALES, ROLE_CSR.
    ROLE_SALES can only see their own records. ROLE_CSR needs to be able to see all records.

    I tried to make a finder, but when I click on it, it does the same thing as if I just clicked create. (full blank submission form, not a list)

    findSalesOrdersBySalesTech.jspx
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <div xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
        <jsp:directive.page contentType="text/html;charset=UTF-8"/>
        <jsp:output omit-xml-declaration="yes"/>
        <form:find finderName="BySalesTech" id="ff_com_aaceglass_domain_SalesOrder" path="/salesorders" z="eYmADvOzqK/rCl9Rg3xkazCQJg8=">
            <field:select disableFormBinding="true" field="salesTech" id="f_com_aaceglass_domain_SalesOrder_salesTech" itemValue="id" items="${salestechnicians}" path="/salestechnicia$
        </form:find>
    </div>
    Last edited by gstewart; Oct 27th, 2012, 05:07 PM. Reason: added finder code

  • #2
    Well.. I'm getting warmer I think. Really could use some help tho.

    From Spring Security Docs.:

    Code:
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
    if (principal instanceof UserDetails) {
      String username = ((UserDetails)principal).getUsername();
    } else {
      String username = principal.toString();
    }
    What imports? Where does it go?

    Code:
    import com.aaceglass.domain.SalesOrder;
    import org.springframework.roo.addon.web.mvc.controller.finder.RooWebFinder;
    import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RequestMapping("/salesorders")
    @Controller
    @RooWebScaffold(path = "salesorders", formBackingObject = SalesOrder.class, delete = false, update = false)
    @RooWebFinder
    public class SalesOrderController {
    
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
      if (principal instanceof UserDetails) {
          String username = ((UserDetails)principal).getUsername();
        } else {
          String username = principal.toString();
        }
    }
    ?
    Last edited by gstewart; Oct 29th, 2012, 02:41 PM. Reason: last attempt was way off

    Comment

    Working...
    X