Implementation of a common permission Control Algorithm in Java

Source: Internet
Author: User

Here I will introduce a very common and professional permission control concept. The description in Java is similar. If you want to switch to another language owner, you can switch it yourself. For convenience, we define a ^ B as the power B of. Here, we set a unique integer for each operation, for example:

Delete A---0
Modify A---1
Add A---2

Delete B---3
Modify B---4
Add B---5

......

Theoretically, there can be n operations, depending on the data type you use to store the user permission value.

This way, if the user has the permission to: Add A---2; Delete B---3; Modify B---4. The user's permission value purview = 2 ^ 2 + 2 ^ 3 + 2 ^ 4 = 28, that is, the sum of the permissions of 2. It can be expressed as 11100 in binary format. In this way, to verify whether the user has the permission to delete B, it can be achieved through bitwise AND operations. In Java, the bitwise and operation operator number is &, that is:

Int value = purview & (INT) math. Pow (2, 3 ));

You will find that when a user has operation permissions, the calculated result will be equal to the required permission value for this operation!

Principle:

Bitwise AND operation:

The preceding formula is used as an example: purview & 2 ^ 3, that is, 28 & 8

Convert them into binary

11100
& 01000
-------------------
01000 = 8 (decimal) = 2 ^ 3

Similarly, if you want to verify that you have the permission to delete the A---0

Available: purview & (INT) math. Pow ));

That is:

11100
& 00001
------------------------
00000 = 0 (decimal )! = 2 ^ 0

One advantage of this algorithm is its speed. You can process n permissions at the same time. If you want to verify that you have permissions to delete both the A---0 and the B---3, you can use purview & (2 ^ 0 + 2 ^ 3) = (2 ^ 0 + 2 ^ 3 )? True: false; Set Multi-Role users. Determine the role of the user based on the permission value.

The following provides a Java code for determining a single operation permission:

// Userpurview is the user's total Permissions
// Optpurview is an operation that requires the permission to be an integer (not authorized !)
Public static Boolean checkpower (INT userpurview, int optpurview)
{
Int purviewvalue = (INT) math. Pow (2, optpurview );
Return (userpurview & purviewvalue) = purviewvalue;
}

Of course, you only need to extend the multi-Permission verification.

Note: first, a system may have many operations. Therefore, create a data dictionary for reference and use it for modification. Second, if you use a database to store user permissions, pay attention to the valid range of values. The operation permission value must be a unique integer!

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.