{"id":18757716,"url":"https://github.com/libotony/node-authority","last_synced_at":"2025-06-22T07:33:39.125Z","repository":{"id":57155966,"uuid":"62689748","full_name":"libotony/node-authority","owner":"libotony","description":"node access control module with mysql","archived":false,"fork":false,"pushed_at":"2017-02-21T09:48:14.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-20T15:07:22.055Z","etag":null,"topics":["authority-control","mysql","node","nodejs-library"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/libotony.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-06T03:58:08.000Z","updated_at":"2018-03-02T10:23:56.000Z","dependencies_parsed_at":"2022-08-30T05:32:10.899Z","dependency_job_id":null,"html_url":"https://github.com/libotony/node-authority","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/libotony/node-authority","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libotony%2Fnode-authority","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libotony%2Fnode-authority/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libotony%2Fnode-authority/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libotony%2Fnode-authority/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/libotony","download_url":"https://codeload.github.com/libotony/node-authority/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/libotony%2Fnode-authority/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260982343,"owners_count":23092583,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authority-control","mysql","node","nodejs-library"],"created_at":"2024-11-07T17:44:25.350Z","updated_at":"2025-06-22T07:33:34.108Z","avatar_url":"https://github.com/libotony.png","language":"JavaScript","readme":"## 权限控制模块\n### 要求\n+ 用户id需要存储在req.session.uid中。\n+ 数据库使用mysql\n+ 数据库中需要有四个表tb_authority、tb_role、tb_map、tb_admin\n\n### 数据库表说明\n表字段可以自由扩展，如下列出的字段不可更改\n\n``` SQL\nCREATE TABLE tb_authority(\n  auth_id     bigint unsigned PRIMARY KEY AUTO_INCREMENT \t  #id，主键，自增\n, name        varchar(40) not null unique                     #权限名\n, code        varchar(40) not null unique                     #权限代码\n, description varchar(80) not null default ''                 #描述\n)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;\n\nCREATE TABLE tb_role(\n  role_id     bigint unsigned PRIMARY KEY AUTO_INCREMENT \t  #id，主键，自增\n, name        varchar(40) not null unique                     #角色名\n, description varchar(80) not null default ''                 #描述\n, authority   varchar(1000) not null default '[]'             #类别权限 数组的json字符串\n)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;\n\nCREATE TABLE tb_map(                                          #管理员-权限组关系表\n  roleid      bigint unsigned NOT NULL                        #tb_role name\n, adminid     bigint unsigned NOT NULL                        #tb_admin id\n)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;\n\nCREATE TABLE tb_admin(                                        #管理表\n  admin_id    bigint unsigned PRIMARY KEY AUTO_INCREMENT  \t  #id,主键,自增\n, username    varchar(50)     NOT NULL DEFAULT '' UNIQUE      #adminname\n, realname    varchar(50)     NOT NULL                        #姓名，必填\n, password    varchar(80)     NOT NULL                        #登录密码，必填\n, disabled    tinyint         NOT NULL DEFAULT 0              #是否停封，默认否\n, adminlevel  TINYINT unsigned NOT NULL DEFAULT 1             #账户等级0,1,2,3,...\n, authority   varchar(1000)   NOT NULL default '[]'           #类别权限 数组的json字符串\n, description varchar(200)    NOT NULL DEFAULT ''             #描述\n)AUTO_INCREMENT=30001 ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;\n```\n### 安装\n``` bash\nnpm install node-authroity --save\n```\n或者在package.json中加入如下字段\n\n``` json\n\"node-authority\": \"^0.1.0\"\n```\n### 初始化\n\n``` javascript\nvar express = require('express');\nvar cookieParser = require('cookie-parser');\nvar redisSession = require('node-redis-session');\n\nvar app = express();\n\n//cookie and session parser\napp.use(cookieParser());\napp.use(redisSession());\n\n// hang middleware after session middleware\nvar authority = require('node-authority');\n// configure authority\nvar dbinfo = {\n\t'host' : 'localhost',\n\t'port' : 3306,\n\t'user' : 'user',\n\t'password' : 'password',\n\t'database' : 'database'\n};\nauthority.configure({mysql:dbinfo});\napp.use(authority.authMiddleWare);\n\napp.listen(3000);\n```\n### 权限举例说明\n`tb_authority.name`为人类易读的权限名称，一般会作为检查权限时传入的参数，`tb_authority.code`为程序储存时的权限名称。`tb_admin.authority`和`tb_role.authority`为权限存储字段，存储方式为`JSON.stringify()`过后的数组。\n### API列表\n* [`check`](#checkpermission)\n\n#### check(permission)\n\n``` javascript\n/**\n * 检查当前用户是否拥有当前指定权限（传入参数）\n * 挂载到res.loacls下方便在页面渲染时使用\n */\nreq.check('Home.View');\nres.locals.check('Home.View');\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibotony%2Fnode-authority","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flibotony%2Fnode-authority","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flibotony%2Fnode-authority/lists"}