The design concept of a typical permission system of struts1 + spring + Hibernate is summarized as follows, which is especially suitable for simple scenarios. For example, if a certain unit has a directly affiliated unit or department
The data table is designed as follows:
User table:
Userid:
Username:
Unitid: // unit ID
Deptid: // department ID
Status: 0 indicates normal user 1: indicates the highest administrator of the Organization
Role role table
ID: No.
Rolename: role name
Content: specific permissions assigned to a role
Memo: Remarks
Unit: unit ID
Userright table: (the user and role are many-to-many, so this table is required)
ID:
Userid:
Roleid:
Unit Table)
Unitid: unit number
Unitname: unit name
Fatherid: ID of the upper-level unit (if it is 0, it is the upper-level unit)
Allfatherid: stores the ID path of all associated parent units)
Unittype: indicates whether the unit is a department, 0: Unit, 1: Department
For example:
Unitid unitname fatherid allfatherid unittype
1 root unit 0 0
2 departments, 1 department, 1 Department
3 directly affiliated units 1 1, 1 0
4 Departments 1 3, 1, 3
Addressgroup unit group table: This table is used to classify several units into one group. For example, when publishing a document, you can send a file to the group at the same time.
All units
Groupid group ID:
Groupname group name
Content: contains the unit ID. For example, a group has a unit of 1, 3.
2. The page layer is similar to the following:
document management
document register
Create
modify
View
....... there are many of the above forms.
use JavaScript to record all selected items
Function check () {
If (thisform. rolename. value = "" | trim (thisform. rolename. value) = "") {
alert ("Enter the role name! ");
thisform. rolename. focus ();
return false;
}< br> thisform. content. value = getrole ();
thisform. submit ();
}< br> // link the role with a comma
function getrole () {
var allchk = document. body. all. tags ('input');
var lstr = "";
for (I = 0; I If (allchk [I]. type. touppercase () = "checkbox" & allchk [I]. checked = true) {
If (lstr = "") {
lstr = allchk [I]. value ;}< br> else {lstr = lstr + "," + allchk [I]. value ;}< BR >}< br> return lstr;
}
We also need to bury a den domain to save it.
<Input type = hidden name = content value = "$! Role. getcontent () ">
Save the three actions Layer
/**
* Save role information
*/
Public void saverole (role) throws exception {
String where = "from role where rolename = '" + role. getrolename () + "'and unitid =" + role. getunit (). getunitid () + "and roleid <>" + role. getroleid ();
List list = roledao. Find (where );
If (list. Size ()> 0 ){
Throw new logicexception ("this unit already exists" + role. getrolename () + "this role ");
}
Roledao. Save (role );
}
4. When editing a role, read a role and display it back on the page.
Public actionforward viewrole (
Actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response ){
String roleid = request. getparameter ("roleid ");
If (! ("". Equals (roleid ))){
Role role = rolemanager. getrole (roleid );
Request. setattribute ("role", role );
}
Return Mapping. findforward ("viewrole ");
}
On the JSP page, you can read it. You can read Init () in the onload function of the body ();
<Input type = hidden name = "content" value = "$! Role. getcontent () ">
Function Init (){
VaR allchk = Document. Body. All. Tags ('input ');
VaR content = thisform. content. value;
If (content! = ""){
VaR strids = content. Split (",");
For (k = 0; k <strids. length; k ++ ){
For (I = 0; I <allchk. length; I ++ ){
If (allchk [I]. type. touppercase () = "checkbox" & allchk [I]. name = "box" & allchk [I]. value = strids [k]) {
Allchk [I]. Checked = true;
}
}
}
}
}
five-Permission judgment
generally in a menu, can be reflected in the navigation menu
below is an example of velocity
# Set ($ recvdoc_list = 111)
# Set ($ recvdoc_sign = 112)
# Set ($ recvdoc_tuihui = 113)
# Set ($ recvdoc_read = 114)
# Set ($ recvdoc_lookfor = 115)
permission encoding for certain functions
Then at the service layer, obtain the permissions of a user:
Public vector getpermissionidsbyuserid (INT userid) throws exception {
Int I;
Vector v = new vector ();
String SQL = "from role where roleid in (select roleid from usersright where userid =" + userid + ")";
List list = roledao. Find (SQL );
Iterator iter = List. iterator ();
While (ITER. hasnext ()){
Role role = (role) ITER. Next ();
String strids [] = stringutils. Split (role. getcontent (),",");
For (I = 0; I <strids. length; I ++ ){
V. Add (strids [I]);
}
}
Return V;
}
}
Public Boolean haspermission (INT permissionid) throws exception {
String permissionidstr = integer. tostring (permissionid );
Boolean retvalue = false;
If (permission = NULL ){
// Return the permission of the user
Permission = RMG. getpermissionidsbyuserid (U. getuserid (). intvalue ());
}
If (permissionidstr. Length () = 3) {retvalue = permission. Contains (permissionidstr );}
If (permissionidstr. Length () = 1)
{
Int I;
For (I = 0; I <permission. Size (); I ++)
{
If (string) Permission. elementat (I). startswith (permissionidstr ))
{
Retvalue = true;
I = permission. Size ();
}
}
}
Return retvalue;
}
Menu blocking judgment
VaR P;
# If ($ userinfo. haspermission (1 ))
P = new createpanel ('recvdoc ', 'document Management ');
# If ($ userinfo. haspermission ($ recvdoc_sign_list) | $ userinfo. haspermission ($ recvdoc_sign_print) | $ userinfo. haspermission ($ recvdoc_sign_read) | $ userinfo. haspermission ($ recvdoc_sign_lookfor ))
P. addbutton ('$ request. getcontextpath ()/menu/images/recvdoc.gif', 'file to be accepted ', 'changepage ("10 ");');
# End
For example, there are many sub-functions in the above receiving management module, which are first passed in to userinfo. haspermission (1) for judgment,
If (permissionidstr. Length () = 1)
{
,
If the user has permissions for a sub-function under the management of the received text, such as 111, the authentication is passed (note I = permission. Size (); out of for, the return value is true, so this
The user can see the large menu "manage text". As for which of the following sub-functions can be seen by the user
If (permissionidstr. Length () = 3) {retvalue = permission. Contains (permissionidstr);} judges.