{"id":168,"date":"2009-11-17T20:20:10","date_gmt":"2009-11-17T18:20:10","guid":{"rendered":"http:\/\/kinsey.no\/blog\/?p=168"},"modified":"2020-03-19T00:12:40","modified_gmt":"2020-03-18T22:12:40","slug":"storing-multiple-privilegessettings-in-a-single-integer","status":"publish","type":"post","link":"https:\/\/kinsey.no\/blog\/index.php\/2009\/11\/17\/storing-multiple-privilegessettings-in-a-single-integer\/","title":{"rendered":"Storing multiple privileges\/settings in a single integer"},"content":{"rendered":"<p>In many applications you have the need to set users privileges , and while there are many ways to do so, I prefer to using a single integer value to store a users privileges on any single entity.<\/p>\n<p>This has many advantages,<\/p>\n<ul>\n<li>you only have a single value representing all privileges<\/li>\n<li>you can add and remove available privileges without altering the schema\/entity model\/storage logic<\/li>\n<li>checking for privileges are very simple<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<p>Now you might be wondering, how is it possible to store any number of privileges in a single integer?<br \/>\nOne of the keys to doing this are the values that you assign each privilege, these must each be twice the previous value.<\/p>\n<p>For example<\/p>\n<ul>\n<li>Read = 1<\/li>\n<li>Write = 2<\/li>\n<li>Delete = 4<\/li>\n<li>&#8230; = 8<\/li>\n<\/ul>\n<p>To understand why we do it like this we need to take a look at the individual integers binary representation<\/p>\n<pre>1 = 00000001\n2 = 00000010\n4 = 00000100\n8 = 00001000\n<\/pre>\n<p>Anyone see the pattern?<\/p>\n<p>And if you were to sum some of these, say Write (2) + Delete(4)<\/p>\n<pre>00000010\n+\n00000100\n=\n00000110\n<\/pre>\n<p>Again, you see the pattern?<\/p>\n<p>Each privilege is being stored as a single bit in the integer, and each bit can be checked against even if you add multiple values together.<\/p>\n<p>So, now that we have some of the theory covered <a style=\"color: rgb(85, 85, 85); text-decoration:none; font-weight: normal\" href=\"https:\/\/kiwigambling.co.nz\/\">kiwi gambling<\/a>, how do we utilize this?<\/p>\n<p>If you have an integer you can easily check for any fraction using a simple statement<\/p>\n<p>In JavaScript<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nvar allPrivileges = 6;\nvar hasWrite = (allPrivileges  == (allPrivileges  | 2));\n<\/pre>\n<p>In VB.net<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\nDim allPrivileges As Integer = 6\nDim hasWrite As Boolean = allPrivileges = (allPrivileges Or 2)\n<\/pre>\n<p>Here we are using the bitwise operator | (<strong>or<\/strong>) to check that allPrivileges (6) binary <strong>or<\/strong>&#8216;ed with 2 equals 6, meaning that 2 is a contained fraction of 6.<\/p>\n<p>Now if you want to add a new privilege (or existing one) you just binary <strong>or <\/strong>the current integer with the new integer<br \/>\nIn JavaScript<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nvar allPrivileges = 4;\nallPrivileges = allprivileges | 2;\n\/\/ allPrivileges equals 6\nallPrivileges= allprivileges | 2;\n\/\/ allPrivileges still equals 6\n<\/pre>\n<p>In VB.net<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\nDim allPrivileges As Integer = 4\nallPrivileges = (allPrivileges Or 2)\n' allPrivileges equals 6\nallPrivileges = (allPrivileges Or 2)\n' allPrivileges still equals 6\n<\/pre>\n<p>Wasn&#8217;t that easy?<\/p>\n<p>In an <a href=\"http:\/\/kinsey.no\/blog\/index.php\/2009\/11\/17\/modifying-privileges-stored-binary-using-extjs\/#more-180\"> upcoming post<\/a> I will show you a simple ExtJs control for visualizing and modifying a users privileges.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In many applications you have the need to set users privileges , and while there are many ways to do so, I prefer to using a single integer value to store a users privileges on any single entity. This has many advantages, you only have a single value representing all privileges you can add and [&hellip;]<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[34],"tags":[48,51,8,49],"_links":{"self":[{"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/posts\/168"}],"collection":[{"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=168"}],"version-history":[{"count":19,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":395,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/posts\/168\/revisions\/395"}],"wp:attachment":[{"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kinsey.no\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}