window._wpemojiSettings = {"baseUrl":"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/","ext":".png","svgUrl":"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/svg\/","svgExt":".svg","source":{"concatemoji":"https:\/\/zyrastory.com\/wp-includes\/js\/wp-emoji-release.min.js?ver=6.1.1"}};
/*! This file is auto-generated */
!function(e,a,t){var n,r,o,i=a.createElement("canvas"),p=i.getContext&&i.getContext("2d");function s(e,t){var a=String.fromCharCode,e=(p.clearRect(0,0,i.width,i.height),p.fillText(a.apply(this,e),0,0),i.toDataURL());return p.clearRect(0,0,i.width,i.height),p.fillText(a.apply(this,t),0,0),e===i.toDataURL()}function c(e){var t=a.createElement("script");t.src=e,t.defer=t.type="text/javascript",a.getElementsByTagName("head")[0].appendChild(t)}for(o=Array("flag","emoji"),t.supports={everything:!0,everythingExceptFlag:!0},r=0;r<o.length;r++)t.supports[o[r]]=function(e){if(p&&p.fillText)switch(p.textBaseline="top",p.font="600 32px Arial",e){case"flag":return s([127987,65039,8205,9895,65039],[127987,65039,8203,9895,65039])?!1:!s([55356,56826,55356,56819],[55356,56826,8203,55356,56819])&&!s([55356,57332,56128,56423,56128,56418,56128,56421,56128,56430,56128,56423,56128,56447],[55356,57332,8203,56128,56423,8203,56128,56418,8203,56128,56421,8203,56128,56430,8203,56128,56423,8203,56128,56447]);case"emoji":return!s([129777,127995,8205,129778,127999],[129777,127995,8203,129778,127999])}return!1}(o[r]),t.supports.everything=t.supports.everything&&t.supports[o[r]],"flag"!==o[r]&&(t.supports.everythingExceptFlag=t.supports.everythingExceptFlag&&t.supports[o[r]]);t.supports.everythingExceptFlag=t.supports.everythingExceptFlag&&!t.supports.flag,t.DOMReady=!1,t.readyCallback=function(){t.DOMReady=!0},t.supports.everything||(n=function(){t.readyCallback()},a.addEventListener?(a.addEventListener("DOMContentLoaded",n,!1),e.addEventListener("load",n,!1)):(e.attachEvent("onload",n),a.attachEvent("onreadystatechange",function(){"complete"===a.readyState&&t.readyCallback()})),(e=t.source||{}).concatemoji?c(e.concatemoji):e.wpemoji&&e.twemoji&&(c(e.twemoji),c(e.wpemoji)))}(window,document,window._wpemojiSettings);
https://zyrastory.com/wp-includes/js/jquery/jquery.min.js
https://zyrastory.com/wp-includes/js/jquery/jquery-migrate.min.js
jQuery(document).ready(function(){document.querySelectorAll(".ez-toc-section").forEach(t=>{t.setAttribute("ez-toc-data-id","#"+decodeURI(t.getAttribute("id")))}),jQuery("a.ez-toc-link").click(function(){let t=jQuery(this).attr("href"),e=jQuery("#wpadminbar"),i=0;30>30&&(i=30),e.length&&(i+=e.height()),jQuery('[ez-toc-data-id="'+decodeURI(t)+'"]').length>0&&(i=jQuery('[ez-toc-data-id="'+decodeURI(t)+'"]').offset().top-i),jQuery("html, body").animate({scrollTop:i},500)})});
(function(c,l,a,r,i,t,y){ c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)}; t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i+"?ref=bwt"; y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y); })(window, document, "clarity", "script", "btkbh92jgl");
Skip to content
今天來講個有關二進位的實務運用(人員權限控管),也是筆者第一個接手的案子用到的技術
CRUD 這幾個字相信大家都不陌生吧,如果不認識的話那改為中文的「增刪查改」 呢?
是電腦中常見的四個動作(更為詳細的還請參照維基 )
那我們這次就以這四個動作針對人員權限控管來舉例
可以看到位數從右到左分別就是CRUD、跟二進位的位數
二進位換算成十進位的方式,這裡就不特別解釋了,詳情可參考維基
那開始正式進入範例
範例說明 #1. 權限控管-設定 小明 是一名資深的銀行業務部專員,具以下權限 「讀取」 、「建立」
那我們該怎麼表示呢
對應第一章圖的總表,大家應該可以輕鬆得出這個答案吧 (二進位表示 0011、十進位數字則為 3 )
至於U跟D則是他沒有的權限所以均為 0
#2. 權限控管-判斷 某天,神秘黑衣人找上小明 ,想要請他幫自己調整帳戶金額 ,成功的話可以給他一大筆錢
於是小明 登入官網嘗試看看修改資料(想也知道他沒有這個權限)
判斷是否有權限是要用AND 來進行計算 ➜ 兩個都要1才會為1
而二進位的計算在SQL中是可以直接這樣子表示,各語言也均有類似的
計算出來的答案為 0 ,故沒有成立(成功有權限的話,結果應該要為 0100 = 22 = 4 )
自然改不了黑衣人的資料
#3. 權限控管- 增加沒辦法達成黑衣人需求,小明 只好直接找上網管,一口氣就要求所有權限
再經過一番死纏爛打之後,網管最終還是同意了幫他新增「U修改」及「D刪除」的權限
要新增權限的話,要用到二進位中的OR 來進行合併 ➜ 任一一個有1 就為1
#4. 權限控管- 減少擁有所有權限的小明 就可以改動帳戶的金額了,幫了黑衣人而且拿到一大筆錢
不料終究紙包不住火,在內部財務稽核的時候還是東窗事發了…
老闆念在小明 沒有功勞也有苦勞,決定只留給他「R查看」 的權限
這裡用到的是二進位的XOR 位元互斥來計算 ➜ 兩個都為1則計算為 0
可以看到在經過處理之後,小明 最終剩下的權限只剩下查看了 (0010 = 21 = 2)
優點及其限制 想到判斷權限,第一個會想到的架構應該會是,一個權限對應一個欄位吧
舉例如下,假設有多個功能要做限制 (Y/N表示有無此權限)
資料庫範例 看起來確實不錯,但要是功能一多就必須要開同等數目的欄位,是不是有一點麻煩
那如果用剛剛講到的二進位來表示呢
看起來是不是簡單了很多!
優點 限制 **受限於數據類型 資料不直觀,需透過權限二進位字典對應 (像是範例最前面的圖1,告知各權限與二進位關聯,可能用DB另一張Table來儲存)
這裡講解一下受限於數據類型 的意思,舉例來說Auth 權限欄位設定為Integer
Integer最大值為 232 -1,故最多只能儲存32個權限
結論 這裡來整理下上面所說的動作及二進位計算的關聯表
針對二進位權限動作的計算方式
這次講解完二進位的權限控管,但其實還是要根據不同的情境來考慮使用
像是若是頁面僅有兩、三個權限,且不會經常更改,那直接用欄位儲存不用二進位計算,可能在效能上反而更好
"Human beings rarely do things for a binary reason"
Laura Kuenssberg
封面照片 : Photo by Danielle Rice on Unsplash
最新資訊文章
有趣程式分享 二進位(binary) , 圖文介紹 , 權限控管 Continue Reading
jQuery(".jq-goTop").click(function (e) {
e.preventDefault();
let hi = jQuery(window).scrollTop();
hi = hi/2.5;
jQuery("html,body").animate(
{
scrollTop: 0,
},
hi
);
window.girl = 0;
var clock = setInterval(testFunc, 150);
setTimeout(function()
{
clearInterval(clock);
jQuery("#walking_girl0").hide();
jQuery("#walking_girl1").hide();
jQuery("#walking_girl2").hide();
},hi);
});function testFunc()
{
//console.log(window.girl);
switch(window.girl)
{
case 0:
jQuery("#walking_girl0").show();
jQuery("#walking_girl1").hide();
jQuery("#walking_girl2").hide();
break;
case 1:
jQuery("#walking_girl0").hide();
jQuery("#walking_girl1").show();
jQuery("#walking_girl2").hide();
break;
case 2:
jQuery("#walking_girl0").hide();
jQuery("#walking_girl1").hide();
jQuery("#walking_girl2").show();
break;
}
window.girl = window.girl == 2 ? 0 : window.girl+1;
}
function closePop(){document.getElementById("headlineatas").style.display = 'none';}
function openPop()
{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent))
{
return false;
}
var r = Math.random();
if(r>0.7 || r<0.3)
{
document.getElementById("headlineatas").style.display = '';
}
}
function getFocus() {
//document.getElementById("wp-block-search__input-1").focus();
document.querySelector("#secondary > form > label > input").focus();
}
(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
js = d.createElement(s); js.id = id;
js.src = 'https://connect.facebook.net/en_US/sdk/xfbml.customerchat.js#xfbml=1&version=v6.0&autoLogAppEvents=1'
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function returnDefault(item)
{
item.innerText = "Copy"
item.style.color = "white"
item.style.backgroundColor = "CornflowerBlue";
}
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent) == false) //20221128 手機用戶移除copy功能
{
jQuery('code').each(function () {
var btn = document.createElement("button");
btn.innerHTML = "Copy";
btn.onmousedown = "event.preventDefault();";
btn.setAttribute('class', 'btnC');
btn.onclick = function(){
var k = this.nextSibling;
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';// We don't need padding, reducing the size if it does flash render.
textArea.style.padding = 0;// Clean up any borders.
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';// Avoid flash of the white box if rendered for any reason.
textArea.style.background = 'transparent';textArea.value = k.textContent;document.body.appendChild(textArea);
textArea.focus();
textArea.select();var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';if(successful)
{
this.focus();
this.style.backgroundColor = "green";
this.innerText = "✔Copied"
openPop();
setTimeout(( ()=>returnDefault(this)),1850)
}document.body.removeChild(textArea);
};var parent = this.parentNode;
parent.insertBefore(btn, this);
});
}
https://zyrastory.com/wp-content/themes/responsiveblogily/js/navigation.js
https://zyrastory.com/wp-content/themes/responsiveblogily/js/skip-link-focus-fix.js
https://zyrastory.com/wp-content/themes/responsiveblogily/js/script.js
https://zyrastory.com/wp-content/themes/responsiveblogily/js/jquery.flexslider.js
https://zyrastory.com/wp-content/themes/responsiveblogily/js/accessibility.js
https://zyrastory.com/wp-includes/js/comment-reply.min.js
https://zyrastory.com/wp-content/plugins/highlighting-code-block/assets/js/prism.js
https://zyrastory.com/wp-content/plugins/highlighting-code-block/build/js/hcb_script.js
https://zyrastory.com/wp-content/plugins/easy-table-of-contents/vendor/js-cookie/js.cookie.min.js
https://zyrastory.com/wp-content/plugins/easy-table-of-contents/vendor/sticky-kit/jquery.sticky-kit.min.js
var ezTOC = {"smooth_scroll":"1","visibility_hide_by_default":"","width":"auto","scroll_offset":"30"};
https://zyrastory.com/wp-content/plugins/easy-table-of-contents/assets/js/front.min.js
(function() {
var expirationDate = new Date();
expirationDate.setTime( expirationDate.getTime() + 31536000 * 1000 );
document.cookie = "pll_language=zh; expires=" + expirationDate.toUTCString() + "; path=/; secure; SameSite=Lax";
}());
/(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};