Limiting Data by User
Limiting Data by User
add new ones. Therefore, assigning the Invoices page to this role is not a valid
solution, because the portlet there allows the user to modify and add invoices. We
must add a new module to consult invoices. Edit the application.xml file of your
project adding the module of listing 13.2.
Listing 13.2 Read only module for Invoice defined in application.xml
<module name="ConsultInvoice">
<env-var name="XAVA_SEARCH_ACTION"
value="CRUD.searchReadOnly"/> <!-- Using this search action the data
is not editable in detail view -->
<model name="Invoice"/>
<controller name="Print"/><!-- Only print actions are available, CRUD is not included -->
</module>
In this simple way we define a read-only module for Invoice. Now, redeploy
your application and configure the security for this new module. During the
course of this chapter you have learned how to do it. You have to:
• Generate portlets and redeploy the application in Liferay (section 13.3).
• Add a new page named “Consult invoices” copying it from “Invoice”
page and put the ConsultInvoice portlet on the new page (section 13.4.4).
• Configure the page permissions making the page accessible by users with
the Seller role only (13.6.2).
Here you see how to give each user type (role) different privileges. Just create
modules with distinct capacities (distinct controllers) and assign them to the
desired roles inside Liferay.
...
@Tabs({
...
Access levels 262
As you can see this module is a read only module that uses the
CurrentCustomer tab to list data. Now redeploy the application, add the menu
entry for this new module and configure its security:
• Generate portlets and redeploy the application in Liferay (section 13.3).
• Add a new page named “My orders” copying it from “Orders” page and
put the CustomerOrders portlet there (section 13.4.4).
• Configure the page permissions to be accessible only by users from
Customer role (13.6.2).
Note that the procedure relies on the fact that the customer number matches
the user id; users have to be created in the portal first, and then the customer has
to be created in the application. The portal's user id must be used as customer
number. The details are shown in figure 13.31.
263 Chapter 13: Security & navigation with Liferay
Control Panel
Invoicing application
This new controller, CustomerOrders, defines the “new” and “save” actions.
Listing 13.6 shows its definition in controllers.xml.
Listing 13.6 CustomerOrders controllers in controllers.xml
<controller name="CustomerOrders">
<!-- Defined like the standard CRUD.new but using a custom action -->
<action name="new"
class="org.openxava.invoicing.actions.NewOrderForCurrentUserAction"
image="images/new.gif"
keystroke="Control N"/>
</controller>
import org.openxava.actions.*;
import org.openxava.util.*; // This package contains the Users class
import org.openxava.view.*;
This action fills the customer data using the user id as a key. When the user
clicks the new button, the customer data will be filled automatically. An
important detail is that the customer view is not editable (2), this way the
searching list for customers is not available and the user cannot access to data of
other customers.
Note how to use Users.getCurrent() (a utility from org.openxava.util
package) to get the currently logged in user. This is a useful tool for limiting the
data visibility programmatically.