SOURCECODE |
How to... add an ActionCapability to a User
Description:
A Capability is used to guard Actions of an application or, and thatīs the way it works, to decide
wether a User is allowed to do the Action or not.
ToDo's:
- Instatiate a new ActionCapability with
- its name
- the text that should be shown in case of an access denial
- the action that is guarded by the Capability
- true or false for the Capability to be set as granted or not
- Add the Capability to the User
Now you can use the Capability as an action where you normally have to define one (like in a MenuItem or a Button) and it will be carried out when the logged on User is granted for it.
Uses:
Shop UserManager User ActionCapability
//The sourcecode is taken from a Shop instance
//imports
import users.*;
//...
//a method to initialize the users of the Shop
public static void init() {
// create a simple User as an example, calling him MasterUser
Customer mu = new Customer("MasterUser");
1
// use the ActionCapability in order to watch a MenuItem, or better the Action of it
ActionCapability officeCap = new ActionCapability(
"Office", //name of the capability
"Access denied to the Office" +
"due to limited Capability", //text on "access denied"
new sale.Action() { //action to be guarded
public void doAction(SaleProcess p, SalesPoint sp) {
//here you put the action that should be guarded
//...
}
},
true); //->hasCapability
//set the DisplayNameResourceBundle so the CapabilityCheckBox can be labled
officeCap.setDisplayNameResourceBundleName("MyResourceBundle");
2
// assign the capability to the MasterUser
mu.setCapability(officeCap);
// add the MasterUser to the UserManager
usermanager.addUser(mu);
//...
// create an ordinary User
Customer ou = new Customer("OrdinaryUser");
2
// assign the capability seen above, but set to false (->access denied)!
ou.setCapability(officeCap.getToggled());
usermanager.addUser(ou);
}
//...
//Somewhere in a method
//The Capability can now be used like an action (which is normally put here...)
//and that action will take place wether the logged on(!) user has the capability or not
msManagerItem = new MenuSheetItem("Open Office",
(ActionCapability)u.getCapability("Office"));