UNPKG

@buession/shiro

Version:

A security framework for JavaScript or TypeScript.

12 lines (11 loc) 2.5 kB
/*! * Buession shiro v2.0.2 * * @link https://shirojs.buession.com/ * @source https://github.com/buession/buession-shirojs * @copyright @ 2020-2025 Buession.com Inc. * @license MIT * @Build Time Fri, 28 Mar 2025 06:21:12 GMT */ !function(s,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports):"function"==typeof define&&define.amd?define(["exports"],i):i((s="undefined"!=typeof globalThis?globalThis:s||self).shiro={})}(this,(function(s){"use strict";const i="*:*",e=s=>!0===(s=>void 0===s)(s)||!0===(s=>null===s)(s),t=(s,i)=>Array.isArray(s)&&s.includes(i),r=(s,i)=>{if(Array.isArray(s)&&Array.isArray(i)&&i.length>0)for(let e=0;e<i.length;e++)if(s.includes(i[e]))return!0;return!1},n=(s,i)=>{if(Array.isArray(s)&&Array.isArray(i)&&i.length>0){for(let e=0;e<i.length;e++)if(!1===s.includes(i[e]))return!1;return!0}return!1};class o{constructor(s,i,e){this.id=s,this.roles=i,this.permissions=e}getId(){return this.id}setId(s){this.id=s}getRoles(){return this.roles}setRoles(s){this.roles=!0===e(s)?[]:s}addRole(s){this.roles.push(s)}addRoles(s){!1===e(s)&&(this.roles=this.roles.concat(s))}getPermissions(){return this.permissions}setPermissions(s){this.permissions=!0===e(s)?[]:s}addPermission(s){this.permissions.push(s)}addPermissions(s){!1===e(s)&&(this.permissions=this.roles.concat(s))}}class l{constructor(s){const i=s;"string"==typeof i.id&&Array.isArray(i.roles)&&Array.isArray(i.permissions)?this.principal=new o(i.id,i.roles,i.permissions):this.principal=s}isAuthenticated(){return null!==this.principal}isNotAuthenticated(){return!1===this.isAuthenticated()}isGuest(){return null===this.principal}isUser(){return null!==this.principal}hasRole(s){return this.isAuthenticated()&&t(this.principal.getRoles(),s)}lacksRole(s){return!1===this.hasRole(s)}hasAnyRole(s){return this.isAuthenticated()&&r(this.principal.getRoles(),s)}hasRolesAll(s){return this.isAuthenticated()&&n(this.principal.getRoles(),s)}hasPermission(s){return this.isAuthenticated()&&r(this.principal.getPermissions(),[i,s])}lacksPermission(s){return!1===this.hasPermission(s)}hasAnyPermission(s){return this.isAuthenticated()&&r(this.principal.getPermissions(),(s||[]).concat(i))}hasAllPermissions(s){return this.isAuthenticated()&&(t(this.principal.getPermissions(),i)||n(this.principal.getPermissions(),s))}hasPermissionsAll(s){return this.hasAllPermissions(s)}}s.ALL_PERMISSION=i,s.Shiro=l,s.default=l,Object.defineProperty(s,"__esModule",{value:!0})})); //# sourceMappingURL=shiro.min.js.map