Clever use of binary authentication permissions

Source: Internet
Author: User

There are multiple permission levels in the permission assignment, and different users have multiple different permissions.

Forum's permissions:

View, post, poll, search

User's permissions:

User A: View, post

User B: viewing

User c: View, post, poll, search

Analysis:

There are four different permission levels, a total of 2^4 types of permissions to assign.

Like this, permission level partitioning and the rights assignment of different levels of users is the best choice by using the binary method of permission assignment authentication. That is, each operation permission is represented by a binary number (1, 10, 100, 1000). The total permission type is 2^n, and N is the permission category. In C #, a long type has 64 bits, so there are 64 categories of permissions, 2^64 how to assign permissions. The allocation, deletion, and viewing of permissions are implemented by binary arithmetic.

Long userrolevalue;//user's permission to operate

Long oprolevalue;//permissions for an action

1. Allocation of permissions (or operations)

Userrolevalue = Userrolevalue | Oprolevalue

(00001110) 2 = (00000010) 2| (00000100) 2| (00001000) # 2

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.