今天老婆终于把我从床上放出来了。。。。。
这两天睡得我是昏天黑地啊。真的只是睡觉哦。但是在我的坚持下,终于还是在床上吧代码写完了
首先我先把心得设计图放出来,大家看看,跟上一版有很大的差别。
然后的。是不是看不清??
给你们细化的一个一个截个图看看
这几个。看清楚了吧?
那么接下来我就用代码给大家讲讲实现吧。
权限类:
这个类主要就是个java bean 每一个此类的实例对象都是一个权限。
package com.dcl.zd.permission;
/**
* @version 1.0
* @created 30-十月-2016 11:21:34
*/
public class Permission {
/**
* 权限名称
*/
private String permissionName;
/**
* 权限编号
*/
private int permissionNum;
/**
* 权限url
*/
private String permissionUrl;
public Permission(){
}
public void finalize() throws Throwable {
}
public String getPermissionName(){
return permissionName;
}
public int getPermissionNum(){
return permissionNum;
}
public String getPermissionUrl(){
return permissionUrl;
}
public void setPermissionName(String name){
this.permissionName=name;
}
public void setPermissionNum(int num){
this.permissionNum=num;
}
public void setPermissionUrl(String url){
this.permissionUrl=url;
}
}
权限集合类:
权限列表类,它储存了一系列的权限,然后他会被 角色类引用代表角色的权限,它还会被userRoleList引用代表此用户的角色所给用户带来的所有权限,此外还会被SystemPermissionList引用,当前系统所有用户的权限都罗列与此。
package com.dcl.zd.permission;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
/**
* @version 1.0
* @created 30-十月-2016 11:21:24
*/
public class PermissionList {
private Map<String,Permission> permissionMap;
private List<Permission> permissionList;
public PermissionList(){
//initialization collection and used synchronized also initialization collection size.
permissionList=Collections.synchronizedList(new ArrayList<Permission>(1200));
permissionMap=Collections.synchronizedMap(new HashMap<String,Permission>());
for(int i=0;i<=1200;i++){
permissionList.add(i ,null);
}
}
public void finalize() throws Throwable {
super.finalize();
}
/**
* add permission to this collection.
* @param permission
*/
public synchronized void addPermission(Permission permission){
permissionList.set(permission.getPermissionNum(),permission);
permissionMap.put(permission.getPermissionUrl(), permission);
}
/**
* add "permission" list all "permission" to this collection.return repetition "permission" number.
* @param permission
*/
public synchronized ArrayList<Integer> addAllPermission(final PermissionList permissions){
ArrayList<Integer> repetitionPermissionNum=new ArrayList<Integer>();
Iterator<Permission> i=permissions.permissionList.iterator();
while(i.hasNext()){
Permission permission=i.next();
if(permission!=null){
repetitionPermissionNum.add(permission.getPermissionNum());
permissionList.set(permission.getPermissionNum(),permission);
permissionMap.put(permission.getPermissionUrl(), permission);
}
}
return repetitionPermissionNum;
}
public List<Permission> getPermissionListData(){
return permissionList;
}
/**
* query permission according permission Number.
* @param num
*/
public Permission getPermissionByNum(int num){
return permissionList.get(num);
}
/**
* query permission according permission URL.
* @param url
*/
public Permission getPermissionByUrl(String url){
return permissionMap.get(url);
}
/**
* removed permission from collection according permission number
* @param num
*/
public synchronized boolean removedPermission(int num){
Permission p=this.getPermissionByNum(num);
permissionMap.remove(p.getPermissionUrl());
permissionList.set(num, null);
return true;
}
/**
* removed a group assign permission.
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-下午9:50:19
*/
public synchronized ArrayList<Integer> removedAnyPermission(ArrayList<Integer> removedList){
ArrayList<Integer> removedFaild=new ArrayList<Integer>();
Iterator<Integer> i=removedList.iterator();
while(i.hasNext()){
int num=i.next();
try{
Permission p=this.getPermissionByNum(num);
permissionMap.remove(p.getPermissionUrl());
permissionList.set(num, null);
}catch(Exception e){
removedFaild.add(num);
}
}
return removedFaild;
}
/**
* get permission number.
*/
public int getPermissionNum(){
return permissionMap.size();
}
/**
* to String used save File
*/
public String toString(){
StringBuffer sb=new StringBuffer();
Set<Entry<String, Permission>> set=permissionMap.entrySet();
Iterator<Entry<String, Permission>> i=set.iterator();
boolean isfirst=true;
while(i.hasNext()){
Entry<String, Permission> entry=i.next();
Permission item=entry.getValue();
if(isfirst){
sb.append(item.getPermissionNum()).append("-").append(item.getPermissionName()).append("-").append(item.getPermissionUrl());
isfirst=false;
}else{
sb.append(",").append(item.getPermissionNum()).append("-").append(item.getPermissionName()).append("-").append(item.getPermissionUrl());
}
}
return sb.toString();
}
/**
* permission String split convert map and list
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年10月31日-下午3:54:48
*/
protected void loadStringToPermission(String permissionString){
String[] permissionListArray=permissionString.split(",");
if(permissionListArray.length>=1&&!permissionListArray[0].equals("")){
for(int i=0;i<permissionListArray.length;i++){
String[] permissionItem=permissionListArray[i].split("-");
Permission p=new Permission();
p.setPermissionName(permissionItem[1]);
p.setPermissionNum(Integer.parseInt(permissionItem[0]));
p.setPermissionUrl(permissionItem[2]);
this.addPermission(p);
}
}
}
/**
* test and look program performance.
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年10月31日-下午4:13:43
*/
public static void main(String args[]){
PermissionList p=new PermissionList();
long nowTime1=System.currentTimeMillis();
for(int i=0;i<=1200;i++){
Permission pitem=new Permission();
pitem.setPermissionName("name"+i);
pitem.setPermissionNum(i);
pitem.setPermissionUrl("url"+i);
p.addPermission(pitem);
}
long nowTime1end=System.currentTimeMillis();
System.out.println(nowTime1end-nowTime1);
Random r=new Random();
int i=r.nextInt(100000);
long nowTime2=System.currentTimeMillis();
Permission p3=p.getPermissionByNum(i);
long nowTime2end=System.currentTimeMillis();
System.out.println(nowTime2end-nowTime2+" "+p3.getPermissionName());
long nowTime3=System.currentTimeMillis();
Permission p4=p.getPermissionByUrl("url"+i);
long nowTime3end=System.currentTimeMillis();
System.out.println(nowTime3end-nowTime3+" "+p4.getPermissionName());
}
}
Roles 角色类:
代表一个角色:
它内部有:
权限列表
角色编号
角色名称
数据文件
拥有此角色的有所用户列表
角色创建的日期
让我在代码里为你解答他们的作用吧。
package com.dcl.zd.permission;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* @version 1.0
* @created 30-十月-2016 11:21:12
*/
public class Roles {
private Date createDate;角色创建的日期
private File dataFile;数据文件
private String name;角色名称
private int num;角色编号
private PermissionList permissions;权限列表
private List<Long> userList;拥有此角色的有所用户列表
//构造方法,会初始化所有信息,并且判断是否已经有了此角色的数据文件,如果没有则创建,如果有则从数据文件中加载数据信息,进行解析在放入相对应的集合。
public Roles(int num){
this.num=num;
permissions=new PermissionList();
/**
* LinkedList is ordered
*/
userList=Collections.synchronizedList(new LinkedList<Long>());
dataFile=new File("permission"+File.separatorChar+"roles"+File.separatorChar+"roles_"+num+".permission");
if(!dataFile.exists()){
this.name="默认角色名称";
this.saveData();
}else{
this.loadData();
}
}
public void finalize() throws Throwable {
super.finalize();
}
//设置角色名称
public void setName(String name){
this.name=name;
this.saveData();
}
/**
通过权限的编号获取权限
* get role permission by permission number.
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-上午10:57:00
*/
public Permission getPermission(int num){
return permissions.getPermissionByNum(num);
}
/**
通过权限的url获取权限
* get role permission by permission url
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-上午10:57:36
*/
public Permission getPermission(String url){
return permissions.getPermissionByUrl(url);
}
/**
添加权限
* add permission to this role.
* @param permissionNum
* @throws IOException
*/
public boolean addPermission(int permissionNum) throws IOException{
Permission p=SystemPermissionList.getSystemPermissionListInstance().getPermissionByNum(permissionNum);
if(p==null||permissions.getPermissionByNum(permissionNum)!=null){
return false;
}else{
permissions.addPermission(p);
Iterator<Long> i=userList.iterator();
while(i.hasNext()){
Long userid=i.next();
UserRoleList user=SystemPermissionList.getSystemPermissionListInstance().getUserById(userid);
user.removedRole(this.num);
user.addRole(this.num);
}
this.saveData();
return true;
}
}
/**
从角色移除权限,并会从所有拥有此角色的用户角色列表中删除此权限
* removed permission from this role.
* @param permissionNum
* @throws IOException
*/
public boolean removedPermission(int permissionNum) throws IOException{
boolean b=permissions.removedPermission(permissionNum);
Iterator<Long> i=userList.iterator();
while(i.hasNext()){
Long userid=i.next();
UserRoleList user=SystemPermissionList.getSystemPermissionListInstance().getUserById(userid);
user.removedRole(this.num);
user.addRole(this.num);
}
this.saveData();
return b;
}
/**
保存数据
* save role information to role file.
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-上午10:58:21
*/
public synchronized boolean saveData(){
File directory=new File("permission"+File.separatorChar+"roles");
if(!directory.exists()){
directory.mkdirs();
}
if(!dataFile.exists()){
try {
dataFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
}
}
try(PrintWriter print=new PrintWriter(new FileOutputStream(dataFile),true)) {
print.println("createDate");
createDate=new Date();
print.println(createDate.getTime());
print.println("name");
print.println(name);
print.println("num");
print.println(num);
print.println("permissions");
print.println(permissions.toString());
print.println("userList");
print.println(userList.toString().replace("[", "").replace("]", "").replace(" ", ""));
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
return true;
}
/**
加载数据
* load role information from role file.
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-上午10:58:49
*/
public boolean loadData(){
File directory=new File("permission"+File.separatorChar+"roles");
if(!directory.exists()){
directory.mkdirs();
}
if(!dataFile.exists()){
try {
dataFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
}
}
try(Scanner read=new Scanner(new FileInputStream(dataFile))) {
while(read.hasNextLine()){
String dataname=read.nextLine();
switch(dataname){
case "createDate":
createDate=new Date(Long.parseLong(read.nextLine()));
break;
case "name":
name=read.nextLine();
break;
case "num":
num=Integer.parseInt(read.nextLine());
break;
case "permissions":
permissions.loadStringToPermission(read.nextLine());
break;
case "userList":
String userListString=read.nextLine();
if(!userListString.equals("")){
String[] userListArray=userListString.split(",");
for(int i=0;i<userListArray.length;i++){
userList.add(Long.parseLong(userListArray[i]));
}
}
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
return true;
}
/**
获取角色编号
* get now object unique number.
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-下午7:09:06
*/
public int getNum(){
return this.num;
}
/**
添加用户,主要被框架内使用
*
*/
protected boolean addUser(final long userId){
if(!userList.contains(userId)){
userList.add(userId);
this.saveData();
return true;
}else{
return false;
}
}
/**
* 移除用户也主要被框架内使用
*/
protected boolean removedUser(final long userId){
if(userList.contains(userId)){
userList.remove(userId);
this.saveData();
return true;
}else{
return false;
}
}
/**
* 获取此角色的权限列表
*/
public PermissionList getPermissionList(){
return permissions;
}
/**
测试了一下
* test load data and test role data save.
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-上午10:59:18
*/
public static void main(String args[]){
Roles role=new Roles(0);
role.loadData();
role.saveData();
System.out.println(role.name);
System.out.println(role.num);
System.out.println(role.permissions);
System.out.println(role.userList);
}
}
SystemPermissionList:系统权限配置列表
此类是个单例,所有的权限都在此类的数据文件中配置,我自己思考应该设计一个注解,然后在每个需要用到权限的方法上进行配置如:@DCLPermission(num="权限编号",url="权限路径标示",name="权限名称"),或者使用Proxy InvocationHandle,然后自动注册进入SystemPermissionList,这样方便开发人员使用(这个后边在细化一下)
package com.dcl.zd.permission;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* @author 董成龙
* @version 1.0
* @created 30-十月-2016 11:20:49
*/
public class SystemPermissionList {
private static SystemPermissionList obj;单例
private int roleNumIndex;角色生成的编号,每次生成一个次数加1,目前没做删除的情况
private PermissionList systemPermissionMap;系统全部权限罗列
private List<Roles> roleList;系统全部角色
private List<Integer> roleArray;系统全部角色编号
private HashMap<Long,UserRoleList> userList; 系统全部权限用户
private List<Long> userListArray;系统全部权限用户Id
private File dataFile;数据文件
下面的自己看看方法名称也就知道了
private SystemPermissionList() throws IOException{
systemPermissionMap=new PermissionList();
roleList=Collections.synchronizedList(new ArrayList<Roles>(1000));
roleArray=Collections.synchronizedList(new LinkedList<Integer>());
userList=new HashMap<>();
userListArray=Collections.synchronizedList(new LinkedList<Long>());
for(int i=0;i<=1000;i++){
roleList.add(i ,null);
}
dataFile=new File("permission"+File.separatorChar+"System.permission");
if(!dataFile.exists()){
roleNumIndex=0;
this.saveData();
}else{
this.loadData();
}
}
public Roles getRoleByNum(int num){
return roleList.get(num);
}
public UserRoleList getUserById(long num){
return userList.get(num);
}
public PermissionList getAllPermission(){
return systemPermissionMap;
}
public List<Roles> getAllRoles(){
return roleList;
}
public synchronized Roles createNewRole(){
int num=this.getroleNumIndex();
Roles role=new Roles(num);
roleList.set(role.getNum(), role);
roleArray.add(role.getNum());
this.saveData();
return role;
}
public synchronized UserRoleList createUserRoleList(long userId){
UserRoleList user=new UserRoleList(userId,this);
if(!userList.containsKey(userId)){
userList.put(userId, user);
userListArray.add(userId);
saveData();
}
return user;
}
public static SystemPermissionList getSystemPermissionListInstance() throws IOException{
if(obj==null){
obj=new SystemPermissionList();
}
return obj;
}
public Permission getPermissionByNum(int num){
return this.systemPermissionMap.getPermissionByNum(num);
}
public Permission getPermissionByUrl(String url){
return this.systemPermissionMap.getPermissionByUrl(url);
}
public void finalize() throws Throwable {
super.finalize();
}
protected synchronized int getroleNumIndex(){
roleNumIndex++;
return roleNumIndex;
}
/**
* save role information to role file.
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-上午10:58:21
*/
public synchronized boolean saveData(){
File directory=new File("permission"+File.separatorChar+"roles");
if(!directory.exists()){
directory.mkdirs();
}
if(!dataFile.exists()){
try {
dataFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
}
}
try(PrintWriter print=new PrintWriter(new FileOutputStream(dataFile),true)) {
print.println("roleNumIndex");
print.println(roleNumIndex);
print.println("systemPermissionMap");
print.println(systemPermissionMap.toString());
print.println("roleArray");
print.println(roleArray.toString().replace("[", "").replace("]", "").replace(" ", ""));
print.println("userListArray");
print.println(userListArray.toString().replace("[", "").replace("]", "").replace(" ", ""));
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
return true;
}
/**
* load role information from role file.
* <p>Description: </p>
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-上午10:58:49
*/
public boolean loadData(){
File directory=new File("permission"+File.separatorChar+"roles");
if(!directory.exists()){
directory.mkdirs();
}
if(!dataFile.exists()){
try {
dataFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
}
}
try(Scanner read=new Scanner(new FileInputStream(dataFile))) {
while(read.hasNextLine()){
String dataname=read.nextLine();
switch(dataname){
case "roleNumIndex":
roleNumIndex=Integer.parseInt(read.nextLine());
break;
case "systemPermissionMap":
String systemPermissionString=read.nextLine();
systemPermissionMap.loadStringToPermission(systemPermissionString);
break;
case "userListArray":
String userListArrayString=read.nextLine();
if(!userListArrayString.equals("")){
String[] userListArrays=userListArrayString.split(",");
for(int i=0;i<userListArrays.length;i++){
userListArray.add(Long.parseLong(userListArrays[i]));
userList.put(Long.parseLong(userListArrays[i]), new UserRoleList(Long.parseLong(userListArrays[i]),this));
}
}
break;
case "roleArray":
String roleArrayString=read.nextLine();
if(!roleArrayString.equals("")){
String[] roleListArray=roleArrayString.split(",");
for(int i=0;i<roleListArray.length;i++){
roleArray.add(Integer.parseInt(roleListArray[i]));
Roles roles=new Roles(Integer.parseInt(roleListArray[i]));
roles.loadData();
roleList.add(Integer.parseInt(roleListArray[i]),roles);
}
}
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
return true;
}
}
UserRoleList:用户角色列表
此类对象用于保存用户的所有角色,以及维护一个用户权限列表
package com.dcl.zd.permission;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
/**
* @author 董成龙
* @version 1.0
* @created 30-十月-2016 11:21:01
*/
public class UserRoleList {
private File dataFile;数据文件
// user "permission" list.
private PermissionList userPermissionList;用户权限列表
// record "permission" repetition emergence in the user role list of number.
private List<Integer> permissionRepetitionNum;用户权限重复的列表,以及重复次数
// the role of the user
private List<Roles> roleList; 角色列表
private List<Integer> roleArray; 角色列表
// user uniqueness flag in database
private long userId;用户唯一标示
省下的比较好理解,自己看看好了,哪里不懂就留言
public UserRoleList(long userid,SystemPermissionList sysPermissionList) {
this.userId = userid;
userPermissionList = new PermissionList();
permissionRepetitionNum = Collections.synchronizedList(new ArrayList<Integer>(1200));
roleList = Collections.synchronizedList(new ArrayList<Roles>(1000));
roleArray = Collections.synchronizedList(new LinkedList<Integer>());
for (int i = 0; i <= 1200; i++) {
permissionRepetitionNum.add(i, 0);
}
for (int i = 0; i <= 1000; i++) {
roleList.add(i, null);
}
dataFile = new File("permission" + File.separatorChar + "userRoles" + File.separatorChar + "user_" + userid
+ ".permission");
if (!dataFile.exists()) {
this.saveData();
} else {
this.loadData(sysPermissionList);
}
}
public void finalize() throws Throwable {
super.finalize();
}
/**
*
* @param num
* @throws IOException
*/
public boolean addRole(int num) {
if(roleList.get(num)==null){
try {
Roles roles = SystemPermissionList.getSystemPermissionListInstance().getRoleByNum(num);
PermissionList p = roles.getPermissionList();
ArrayList<Integer> repetitionPermissionNumber = userPermissionList.addAllPermission(p);
Iterator<Integer> i = repetitionPermissionNumber.iterator();
while (i.hasNext()) {
Integer repetitionNum = i.next();
permissionRepetitionNum.set(repetitionNum, (permissionRepetitionNum.get(repetitionNum) + 1));
}
roleList.set(roles.getNum(),roles);
roleArray.add(roles.getNum());
roles.addUser(userId);
saveData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}else{
return false;
}
}
/**
*
* @param url
*/
public boolean existPermission(String url) {
return userPermissionList.getPermissionByUrl(url)!=null;
}
/**
*
* @param num
*/
public boolean existPermission(int num) {
return userPermissionList.getPermissionByNum(num)!=null;
}
/**
*
* @param num
*/
public boolean existRole(int num) {
return roleList.get(num)!=null;
}
/**
* removed role from UserRoleList and removed role of permission from user permission list.
* @param num
*/
public synchronized boolean removedRole(int num) {
if(roleArray.contains(num)){
ArrayList<Integer> removedNoRepetition=new ArrayList<Integer>();
Roles roles=roleList.get(num);
roleList.set(roles.getNum(), null);
roleArray.remove(Integer.valueOf(roles.getNum()));
PermissionList rpermission =roles.getPermissionList();
List<Permission> plist=rpermission.getPermissionListData();
Iterator<Permission> i=plist.iterator();
while(i.hasNext()){
Permission pitem=i.next();
if(pitem!=null){
int permissionNum=permissionRepetitionNum.get(pitem.getPermissionNum());
if(permissionNum<=1){
permissionRepetitionNum.set(pitem.getPermissionNum(), 0);
removedNoRepetition.add(pitem.getPermissionNum());
}else{
permissionRepetitionNum.set(pitem.getPermissionNum(), permissionNum-1);
}
}
}
ArrayList<Integer> noRemovedPermission=userPermissionList.removedAnyPermission(removedNoRepetition);
saveData();
return noRemovedPermission.size()==0;
}else{
return false;
}
}
/**
* save role information to role file.
* <p>
* Description:
* </p>
*
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-上午10:58:21
*/
public synchronized boolean saveData() {
File directory = new File("permission" + File.separatorChar + "userRoles");
if (!directory.exists()) {
directory.mkdirs();
}
if (!dataFile.exists()) {
try {
dataFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
}
}
try (PrintWriter print = new PrintWriter(new FileOutputStream(dataFile), true)) {
print.println("userId");
print.println(userId);
print.println("userPermissionList");
print.println(userPermissionList.toString());
print.println("roleArray");
print.println(roleArray.toString().replace("[", "").replace("]", "").replace(" ", ""));
print.println("permissionRoleHiveNum");
print.println(permissionRepetitionNum.toString().replace("[", "").replace("]", "").replace(" ", ""));
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
return true;
}
/**
* load role information from role file.
* <p>
* Description:
* </p>
*
* @author 董成龙
* @email 8261425@qq.com
* @date 2016年11月1日-上午10:58:49
*/
public boolean loadData(SystemPermissionList s) {
File directory = new File("permission" + File.separatorChar + "roles");
if (!directory.exists()) {
directory.mkdirs();
}
if (!dataFile.exists()) {
try {
dataFile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
}
}
try (Scanner read = new Scanner(new FileInputStream(dataFile))) {
while (read.hasNextLine()) {
String dataname = read.nextLine();
switch (dataname) {
case "userId":
userId = Long.parseLong(read.nextLine());
break;
case "userPermissionList":
String systemPermissionString = read.nextLine();
userPermissionList.loadStringToPermission(systemPermissionString);
break;
case "roleArray":
String roleArrayString = read.nextLine();
if (!roleArrayString.equals("")) {
String[] roleListArray = roleArrayString.split(",");
for (int i = 0; i < roleListArray.length; i++) {
roleArray.add(Integer.parseInt(roleListArray[i]));
Roles roles = s.getRoleByNum(Integer.parseInt(roleListArray[i]));
roleList.set(roles.getNum(), roles);
}
}
break;
case "permissionRoleHiveNum":
String userListArrayString = read.nextLine();
if (!userListArrayString.equals("")) {
String[] userListArrays = userListArrayString.split(",");
for (int i = 0; i < userListArrays.length; i++) {
permissionRepetitionNum.set(i,
Integer.parseInt(userListArrays[i]));
}
}
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
return true;
}
public static void main(String args[]) {
SystemPermissionList l;
try {
l = SystemPermissionList.getSystemPermissionListInstance();
/**
* 增加角色
*/
Roles r1 = l.getRoleByNum(1);
if(r1==null){
r1=l.createNewRole();
}
/**
* 给角色添加权限
*/
r1.addPermission(1);
r1.addPermission(2);
/**
*通过用户id创建一个用户权限
*/
UserRoleList ur1=l.createUserRoleList(1);
/**
* 向用户添加角色
*/
//ur1.addRole(r1.getNum());
System.out.println(ur1.existPermission(1));
/**
* 删除角色从用户
*/
ur1.removedRole(r1.getNum());
/**
* 查询全部角色
*/
l.getAllRoles();
/**
* 用权限num判断用户是否有某一个权限
*/
System.out.println(ur1.existPermission(1));
/**
* 用url判断用户是否有某一个权限
*/
ur1.existPermission("");
/**
* 通过角色号判断用户手否有某一个角色
*/
ur1.existRole(2);
ur1.existRole(1);
/**
/*
Roles r2=l.getRoleByNum(1);
ur1.addRole(r2.getNum());*/
/*UserRoleList ur1=l.getUserById(12);
ur1.removedRole(1);*/
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
欢迎大家留言啊。这样我才有动力继续写啊。。
好了大家拜拜,
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于