052 Medical Program-Module Five: Permission settings-implementation of different menus based on user display

Source: Internet
Author: User

Our previous practice was to:

No matter what type of user is logged in: Hospital, health Bureau, etc. the left menu is the same. In this article we are going to implement different user login when the menu displayed is different.

Let's start with the tables in the database: those tables are related to third-party systems.

These tables correspond to the

What we're looking for is a two-level menu under a character's menu and menu:

Let's go to the menu table first:

We see Bss_sys_module is the Menu table (module table):

This table contains a level one and a level two menu. We want to find out the first level and level two menu:

 select  m1.moduleid,m1.name,m2.moduleid menuid_two,m2.name menuname_two, M2.url from   Bss_sys_module m1,bss_sys_module m2  where  m1.parentid=  "  0   " --  guaranteed to find a first-level menu  and  M2.parentid!=   "  0   " --  Guaranteed to find the level two menu  and  m2.parentid=  M1.moduleid--  Isolate Level two menu under menu  

Results:

But what we're going to look at is: the module under the character, so the final SQL we're going to write:

SelectM1.moduleid,m1.name,m2.moduleid Menuid_two,m2.name Menuname_two,m2.url frombss_sys_module m1,bss_sys_module m2whereM1.parentid='0'--guaranteed to find out the first-level menu . andM2.parentid!='0'--guaranteed to check out the level two menu . andM2.parentid=M1.moduleid--Find the level two menu under the first level menu andM2.moduleidinch(--menu within a role rangeSelectBss_sys_rolemodule.moduleid frombss_sys_role,bss_sys_rolesys,bss_sys_rolenode,bss_sys_rolemodule,bss_sys_roleoperatewhereBss_sys_role.roleid=Bss_sys_rolesys.roleid--know which system the character is in. andBss_sys_rolenode.rsid=Bss_sys_rolesys.rsid--find nodes based on system andBss_sys_rolemodule.rnid=Bss_sys_rolenode.rnid--model under a node andBss_sys_rolemodule.rmid=Bss_sys_roleoperate.rmid andBss_sys_role.roleid='a1657d9c6c7d47b59a99132a5ace1a2e' --designated Bureau of Health Inspection)   Order  byM1.showorder,m2.showorder

Query Result:

This is the module authority of the Health Bureau.

Similarly, let's look at the supplier's menu permissions:

SelectM1.moduleid,m1.name,m2.moduleid Menuid_two,m2.name Menuname_two,m2.url frombss_sys_module m1,bss_sys_module m2whereM1.parentid='0'--guaranteed to find out the first-level menu . andM2.parentid!='0'--guaranteed to check out the level two menu . andM2.parentid=M1.moduleid--Find the level two menu under the first level menu andM2.moduleidinch(--menu within a role rangeSelectBss_sys_rolemodule.moduleid frombss_sys_role,bss_sys_rolesys,bss_sys_rolenode,bss_sys_rolemodule,bss_sys_roleoperatewhereBss_sys_role.roleid=Bss_sys_rolesys.roleid--know which system the character is in. andBss_sys_rolenode.rsid=Bss_sys_rolesys.rsid--find nodes based on system andBss_sys_rolemodule.rnid=Bss_sys_rolenode.rnid--model under a node andBss_sys_rolemodule.rmid=Bss_sys_roleoperate.rmid andBss_sys_role.roleid='dd5e37f61b4d4d79be84c3b8fceddaf0' --supplier of designated check)   Order  byM1.showorder,m2.showorder

The results are as follows:

Write the SQL and we'll write mapper.

Here is a question: We look at the first level menu corresponding to several two-level menu, that is, a one-to-many relationship, so we should be aware of the mapping, we have learned mybatis before learning is a mybatis one-to-many queries,

We're going to use this method here. With Resultmap.

 <!--"Menus" refers to the list of properties inside the menu class<Menu>menus. Since five query results are returned in the SELECT statement, we want to map the five returned results one by one. Here is the mapping.  column represents the columns in the database, and the property represents the attributes in the Yycg.base.pojo.vo.menu class.             --> <Resultmap ID="FindmenubyroleidresultmapThe type="Yycg.base.pojo.vo.Menu">    <Idcolumn="MenuID" Property="MenuID"/>----This represents the primary key, which means that MenuID is the same as merging     <Resultcolumn="Menuname" Property="Menuname"/>    <Collection Property="Menus" ofType="Yycg.base.vo.Menu">        <Resultcolumn="Menuid_two" Property="MenuID"/>         <Resultcolumn="Menuname_two" Property="Menuname"/>         <Resultcolumn=The "URL" property="url"/>    </Collection></Resultmap> 






<!--get menu by role (secondary)-- <SelectId="Findmenubyroleid" ParameterType="Java.lang.String" Resultmap="Findmenubyroleidresultmap"> SelectM1.moduleid MenuID, M1.name menuname, M2.moduleid menuid_two, M2.name Menuname_two, M 2.url frombss_sys_module M1, bss_sys_module m2whereM2.parentid=M1.moduleid andM1.parentid= '0' andM2.parentid!= '0' andM2.moduleidinch( <!--Role-scoped menus- SelectBss_sys_rolemodule.moduleid frombss_sys_role,bss_sys_rolesys,bss_sys_rolenode,bss_sys_rolemodule,bss_sys_roleoperatewhereBss_sys_role.roleid=Bss_sys_rolesys.roleid andBss_sys_rolenode.rsid=Bss_sys_rolesys.rsid andBss_sys_rolemodule.rnid=Bss_sys_rolenode.rnid andBss_sys_roleoperate.rmid=Bss_sys_rolemodule.rmid andBss_sys_role.roleid=#{roleid})Order byM1.showorder,m2.showorder</Select>

After we have written the DAO layer, let's write the service layer:

        //Remove the role ID because the user's groupid is the user's role ID, so you can directly//The data dictionary gets GroupID , which is the role ID.String Roleid=systemconfigservice.finddictinfobydictcode ("S01", Sysuser.getgroupid ()). Getremark ();            ACTIVEUSER.SETSYSMC (SYSMC); //get menu based on role ID//Here's a point we're taking out is a list but we're going to have a menu inside//This is how it is done, and when we design json again, we consider this: menus:[//]List<menu> menu_list =Sysusermappercustom.findmenubyroleid (Roleid); Menu Menu=NewMenu ();            Menu.setmenus (menu_list); Activeuser.setmenu (menu);//Save User menu in user identity object

Get the menu based on the role ID//Here When we fetch menu, it involves a difficult point we take out is a list but we have to exist a menu inside//This How to do, we re-design JSON is to consider this: menus:[//]

After the service has been written, we'll write the action.

    /**      *      * get menu menu and convert     to JSON*    /@RequestMapping ("/usermenu")      public @ResponseBody  Menu Usermenu (httpservletrequest request)throws  Exception    {                HttpSession session=request.getsession ();                Activeuser Activeuser=(activeuser) Session.getattribute (config.activeuser_key);                 return     Activeuser.getmenu ();                            }

We get JSON data by entering http://127.0.0.1:8080/yycgproject/usermenu.action in the URL of the browser.

Finally make changes on the page:

var _menus; $ (function () {//pre-load method//request a menu via Ajax$.ajax ({ URL: ' ${baseurl}usermenu.action ' , type:' POST ', DataType:' JSON ', Success:function (data) {_menus=data; Initmenu (_menus);//parse the JSON data to generate the menu}, Error:function (msg) {alert (' Menu loading exception! '); }        });

Test: After login with admin: The effect is as follows:

Only the Super Administrator's menu is displayed, and no other menus are displayed.

Test passed.

052 Medical Program-Module Five: Permission settings-implementation of different menus based on user display

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.