Skip to content

Commit

Permalink
调整兼容性
Browse files Browse the repository at this point in the history
  • Loading branch information
yong.teng committed Dec 4, 2021
1 parent 49c5f08 commit 8f25c6a
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 58 deletions.
8 changes: 8 additions & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
>= 1%
last 2 versions
Explorer >= 8
Edge >= 12
Firefox >= 21
Chrome >= 23
Safari >= 10
Opera >= 10
Android >= 3
IOS >= 7
not dead
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
<div align="center">
<p align="center">Typescript 或者 JavaScript 的权限验证库</p>
<a href="https://codecov.io/github/buession/buession-shirojs?branch=master">
<img
src="https://img.shields.io/codecov/c/github/buession/buession-shirojs?token=NW2XVQWGPP"
alt="Coverage Status"
/>
<img src="https://img.shields.io/codecov/c/github/buession/buession-shirojs?token=NW2XVQWGPP" alt="Coverage Status" />
</a>
<a href="https://www.npmjs.com/package/buession-shirojs">
<img src="https://img.shields.io/bundlephobia/minzip/buession-shirojs/latest" alt="Size" />
Expand Down Expand Up @@ -60,9 +57,9 @@ yarn add buession-shirojs
```vue
<template>
<div>
<div v-if="shiro.hasRole('admin)">Admin</div>
<div v-if="shiro.hasRole('admin')">Admin</div>
<div v-if="shiro.hasPermission('user:edit')">Edit</div>
<div v-if="shiro.hasPermissions('user:add', 'user:edit')">Action</div>
<div v-if="shiro.hasPermissions(['user:add', 'user:edit'])">Action</div>
</div>
</template>
Expand Down
295 changes: 294 additions & 1 deletion package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "buession-shirojs",
"alias": "shirojs",
"version": "v1.0.0",
"version": "v1.0.1",
"description": "A security framework for JavaScript or TypeScript.",
"homepage": "https://github.com/buession/buession-shirojs",
"homepage": "https://buession.github.io/buession-shirojs/",
"author": {
"name": "yong.teng",
"email": "[email protected]"
Expand All @@ -20,7 +20,7 @@
"scripts": {
"build": "npm cache clean --force && rollup -c build/rollup.config.js",
"lint": "eslint -c ./.eslintrc.js './{src,scripts,build}/**/*.{js,ts,tsx}'",
"release": "yarn build && yarn publishing"
"release": "yarn build && yarn publish"
},
"keywords": [
"buession",
Expand All @@ -42,6 +42,7 @@
],
"dependencies": {},
"devDependencies": {
"@babel/core": "^7.16.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.0",
"@babel/plugin-syntax-jsx": "^7.16.0",
"@rollup/plugin-json": "^4.1.0",
Expand Down
1 change: 1 addition & 0 deletions src/core/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ALL_PERMISSION = ':';
37 changes: 20 additions & 17 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export type T_STRING = string | null | undefined;
export type T_STRING_ARRAY = string[] | null | undefined;

export interface Principal {

/**
Expand All @@ -13,7 +16,7 @@ export interface Principal {
* @param id
* 用户 ID
*/
setId(id:string): void,
setId(id: string): void,

/**
* 返回用户角色名称列表
Expand All @@ -28,23 +31,23 @@ export interface Principal {
* @param roleNames
* 用户角色名称列表
*/
setRoles(roleNames:string[]): void,
setRoles(roleNames: string[]): void,

/**
* 添加用户角色
*
* @param roleName
* 用户角色名称
*/
addRole(roleName:string): void,
addRole(roleName: string): void,

/**
* 批量添加用户角色
*
* @param roleNames
* 用户角色名称列表
*/
addRoles(roleNames:string[]): void,
addRoles(roleNames: string[]): void,

/**
* 返回用户权限列表
Expand All @@ -59,26 +62,26 @@ export interface Principal {
* @param permissions
* 用户权限列表
*/
setPermissions(permissions:string[]): void,
setPermissions(permissions: string[]): void,

/**
* 添加用户权限
*
* @param permission
* 用户权限
*/
addPermission(permission:string): void,
addPermission(permission: string): void,

/**
* 批量添加用户权限
*
* @param permissions
* 用户权限列表
*/
addPermissions(permissions:string[]): void
addPermissions(permissions: string[]): void
}

export interface Shiro {
export interface IShiro {

/**
* 验证是否为已认证通过的用户,不包含已记住的用户,这是与 isUser 标签方法的区别所在
Expand Down Expand Up @@ -116,7 +119,7 @@ export interface Shiro {
*
* @return 用户是否具备某角色
*/
hasRole(roleName:string): boolean;
hasRole(roleName: string): boolean;

/**
* 验证用户是否不具备某角色,与 hasRole 逻辑相反
Expand All @@ -126,7 +129,7 @@ export interface Shiro {
*
* @return 用户是否不具备某角色
*/
lacksRole(roleName:string): boolean;
lacksRole(roleName: string): boolean;

/**
* 验证用户是否具有以下任意一个角色
Expand All @@ -136,17 +139,17 @@ export interface Shiro {
*
* @return 用户是否具有以下任意一个角色
*/
hasAnyRole(roleNames:string[]): boolean;
hasAnyRole(roleNames: string[]): boolean;

/**
* 验证用户是否具有以下所有角色
* 验证用户是否具有以下所有角色
*
* @param roleNames
* 角色列表
*
* @return 用户是否具有以下所有角色
*/
hasRolesAll(roleNames:string[]): boolean;
hasRolesAll(roleNames: string[]): boolean;

/**
* 验证用户是否具备某权限
Expand All @@ -156,7 +159,7 @@ export interface Shiro {
*
* @return 用户是否具备某权限
*/
hasPermission(permission:string): boolean;
hasPermission(permission: string): boolean;

/**
* 验证用户是否不具备某权限,与 hasPermission 逻辑相反
Expand All @@ -166,7 +169,7 @@ export interface Shiro {
*
* @return 用户是否不具备某权限
*/
lacksPermission(permission:string): boolean;
lacksPermission(permission: string): boolean;

/**
* 验证用户是否具有以下任意一个权限
Expand All @@ -176,7 +179,7 @@ export interface Shiro {
*
* @return 用户是否具有以下任意一个权限
*/
hasAnyPermission(permissions:string[]): boolean;
hasAnyPermission(permissions: string[]): boolean;

/**
* 验证用户是否具有以下所有权限
Expand All @@ -186,6 +189,6 @@ export interface Shiro {
*
* @return 用户是否具有以下所有权限
*/
hasPermissionsAll(permissions:string[]): boolean;
hasPermissionsAll(permissions: string[]): boolean;

}
6 changes: 4 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ShiroImpl as Shiro } from './shiro';
import { IShiro } from './shiro';

export declare const Shiro: ShiroImpl;
export { Principal } from './core/types';

export declare const Shiro: IShiro;

export default Shiro;
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ShiroImpl as Shiro } from './shiro';
import { Shiro } from './shiro';

export { Principal } from './core/types';

export default Shiro;
49 changes: 25 additions & 24 deletions src/shiro.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { Principal, Shiro } from './core/types'

import { ALL_PERMISSION } from './core/constants'
import { Principal, IShiro } from './core/types'
import { hasAny, hasAll } from './utils/common'

export class ShiroImpl implements Shiro {

private readonly $principal:Principal;
export class Shiro implements IShiro {

constructor(public principal:Principal) {
this.$principal = principal;
constructor(public readonly principal: Principal) {
}

/**
Expand All @@ -16,7 +13,7 @@ export class ShiroImpl implements Shiro {
* @return 用户是否已通过认证
*/
isAuthenticated(): boolean {
return this.$principal !== null;
return this.principal !== null;
}

/**
Expand All @@ -34,7 +31,7 @@ export class ShiroImpl implements Shiro {
* @return 用户是否为访客
*/
isGuest(): boolean {
return this.$principal === null;
return this.principal === null;
}

/**
Expand All @@ -43,7 +40,7 @@ export class ShiroImpl implements Shiro {
* @return 用户是否认证通过或已记住的用户
*/
isUser(): boolean {
return this.$principal !== null;
return this.principal !== null;
}

/**
Expand All @@ -54,8 +51,8 @@ export class ShiroImpl implements Shiro {
*
* @return 用户是否具备某角色
*/
hasRole(roleName:string): boolean {
return this.isAuthenticated() && hasAny(this.$principal.getRoles(), roleName);
hasRole(roleName: string): boolean {
return this.isAuthenticated() && hasAny(this.principal.getRoles(), roleName);
}

/**
Expand All @@ -66,7 +63,7 @@ export class ShiroImpl implements Shiro {
*
* @return 用户是否不具备某角色
*/
lacksRole(roleName:string): boolean {
lacksRole(roleName: string): boolean {
return this.hasRole(roleName) === false;
}

Expand All @@ -78,13 +75,13 @@ export class ShiroImpl implements Shiro {
*
* @return 用户是否具有以下任意一个角色
*/
hasAnyRole(roleNames:string[]): boolean {
hasAnyRole(roleNames: string[]): boolean {
if (this.isAuthenticated() == false) {
return false;
}

for (let roleName of roleNames) {
if (hasAny(this.$principal.getRoles(), roleName)) {
if (hasAny(this.principal.getRoles(), roleName)) {
return true;
}
}
Expand All @@ -100,8 +97,8 @@ export class ShiroImpl implements Shiro {
*
* @return 用户是否具有以下所有角色
*/
hasRolesAll(roleNames:string[]): boolean {
return this.isAuthenticated() && hasAll(this.$principal.getRoles(), roleNames);
hasRolesAll(roleNames: string[]): boolean {
return this.isAuthenticated() && hasAll(this.principal.getRoles(), roleNames);
}

/**
Expand All @@ -112,8 +109,8 @@ export class ShiroImpl implements Shiro {
*
* @return 用户是否具备某权限
*/
hasPermission(permission:string): boolean {
return this.isAuthenticated() && hasAny(this.$principal.getPermissions(), permission);
hasPermission(permission: string): boolean {
return this.isAuthenticated() && (hasAny(this.principal.getPermissions(), permission) || hasAny(this.principal.getPermissions(), ALL_PERMISSION));
}

/**
Expand All @@ -124,7 +121,7 @@ export class ShiroImpl implements Shiro {
*
* @return 用户是否不具备某权限
*/
lacksPermission(permission:string): boolean {
lacksPermission(permission: string): boolean {
return this.hasPermission(permission) === false;
}

Expand All @@ -136,13 +133,17 @@ export class ShiroImpl implements Shiro {
*
* @return 用户是否具有以下任意一个权限
*/
hasAnyPermission(permissions:string[]): boolean {
hasAnyPermission(permissions: string[]): boolean {
if (this.isAuthenticated() == false) {
return false;
}

if (hasAny(this.principal.getPermissions(), ALL_PERMISSION)) {
return true;
}

for (let permission of permissions) {
if (hasAny(this.$principal.getPermissions(), permission)) {
if (hasAny(this.principal.getPermissions(), permission)) {
return true;
}
}
Expand All @@ -158,8 +159,8 @@ export class ShiroImpl implements Shiro {
*
* @return 用户是否具有以下所有权限
*/
hasPermissionsAll(permissions:string[]): boolean {
return hasAll(this.$principal.getPermissions(), permissions);
hasPermissionsAll(permissions: string[]): boolean {
return hasAny(this.principal.getPermissions(), ALL_PERMISSION) || hasAll(this.principal.getPermissions(), permissions);
}

}
6 changes: 3 additions & 3 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
type FindTypes = string[] | null | undefined;
import { T_STRING, T_STRING_ARRAY } from '../core/types'

export const hasAny = (data:FindTypes, value:any) => {
export const hasAny = (data: T_STRING_ARRAY, value: any, all:T_STRING = null): boolean => {
return typeof data !== 'undefined' && data != null ? data.indexOf(value) >= 0 : false;
}

export const hasAll = (data:FindTypes, values:any[]) => {
export const hasAll = (data: T_STRING_ARRAY, values: any[], all:T_STRING = null): boolean => {
if (typeof data !== 'undefined' && data != null) {
for (let value of values) {
if (data.indexOf(value) < 0) {
Expand Down
Loading

0 comments on commit 8f25c6a

Please sign in to comment.