问题描述
在附加的程序中,我只想获得输出的第一部分。它应该是这样的:
employee:(Permissions: [rw public,r secret],Subjects: [alice,bob],Parents: [])
manager:(Permissions: [w secret,r top-secret],Subjects: [carol],Parents: [employee])
ceo:(Permissions: [w top-secret],Subjects: [eve],Parents: [manager])
但是当我运行附加的程序时,我得到以下输出:
rw public r secret alice bob
rw public r secret alice bob w secret r top-secret carol
rw public r secret alice bob w secret r top-secret carol w top-secret eve
我知道这与第一个 toString 方法有关,但我不知道为什么我无法获得正确的格式。此外,当我运行当前的 toString 方法时,addParent 方法虽然正确,但根本不显示。所有更改都需要在### 类中进行...我认为。如果你能帮忙,请告诉我。在此先感谢您的帮助!
代码如下:
package lecture9
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/*
* A role contains a list of permission objects
* a list of subjects (as strings)
* a list of parents (which roles does this role inherits from)
* a role name
*/
class Role {
List<Role> parents = new ArrayList<>();
List<Permission> permissions = new ArrayList<>();
List<String> subjects = new ArrayList<>();
String name;
// a role must have a name
Role(String name) {
this.name = name;
}
// add parent role and return 'this' object (to allow method chaining)
Role addParent(Role parent) {
parents.add(parent);
return this;
}
// add permission object and return 'this' object
Role addPermission(Permission permission) {
permissions.add(permission);
return this;
}
// add subject (so that the 'subject' will have this role) and return 'this' object
Role addSubject(String subject) {
subjects.add(subject);
return this;
}
// check whether this role has the permission that grants access 'rights' to 'object'
/*boolean checkPermission(String object,String rights) {
// Todo
}
// check whehther 'subject' has the access 'rights' to 'objects'
boolean checkPermission(String subject,String object,String rights) {
// Todo
}*/
// return a string representation of this role (need to include all information)
public String toString() {
String par = "";
String per = "";
String sub = "";
for(Role r: parents) {
par = par + " " + r + " ";
}
for(Permission p : permissions){
per = per + " " + p + " ";
}
for(String s : subjects) {
sub = sub + " " + s + " ";
}
return par + per + sub;
}
}
/*
* permission object specifies the access 'rights' to an 'object'
* both 'rights' and 'object' are strings
*/
class Permission {
final String object;
final String rights;
Permission(String object,String rights) {
this.object = object;
this.rights = rights;
}
// return true if 'object' is the 'object' in this permission and 'rights' is a substring of the 'rights' in this permission
boolean checkPermission(String object,String rights) {
return this.object.equals(object) && this.rights.indexOf(rights) >= 0;
}
// return string representation of this permission object
public String toString() {
return rights + " " + object;
}
}
/*
* represents a RBAC policy
*/
public class RBAC {
// stores all roles in a list
List<Role> roles = new ArrayList<Role>();
// add a new role to the list
RBAC addRole(Role role) {
roles.add(role);
return this;
}
// check whether 'subject' has the access 'rights' to 'object'
/*boolean checkPermission(String subject,String rights) {
// Todo
}*/
// return string representation of the RBAC policy
public String toString() {
String ret = "";
for(Role r: roles) {
ret = ret + r + "\n";
}
return ret;
}
public static void main(String[] args) {
RBAC rbac = new RBAC();
Role employee = new Role("employee")
.addSubject("alice")
.addSubject("bob")
.addPermission(new Permission("public","rw"))
.addPermission(new Permission("secret","r"));
Role manager = new Role("manager")
.addSubject("carol")
.addPermission(new Permission("secret","w"))
.addPermission(new Permission("top-secret","r"))
.addParent(employee);
Role ceo = new Role("ceo")
.addSubject("eve")
.addPermission(new Permission("top-secret","w"))
.addParent(manager);
rbac.addRole(employee).addRole(manager).addRole(ceo);
System.out.println(rbac);
/*Scanner in = new Scanner(system.in);
System.out.println("Enter your command (e.g. alice r secret):");
String cmd = in.nextLine().trim();
while(!cmd.equals("")) {
String[] triple = cmd.split(" ");
if(triple.length != 3) {
System.out.println("Illegal command. Try again");
}
else {
String subj = triple[0],op = triple[1],obj = triple[2];
try {
if(rbac.checkPermission(subj,obj,op)) {
System.out.printf("%s is allowed to perform %s operation on %s\n",subj,op,obj);
}
else {
System.out.printf("%s is NOT allowed to perform %s operation on %s\n",obj);
}
} catch(Exception e) { System.out.println(e); }
}
System.out.println("\nEnter your command");
cmd = in.nextLine().trim();
}
in.close();*/
}
}
解决方法
问题出在 Role.toString:
改变这个:
public String toString() {
String par = "";
String per = "";
String sub = "";
for(Role r: parents) {
par = par + " " + r + " ";
}
for(Permission p : permissions){
per = per + " " + p + " ";
}
for(String s : subjects) {
sub = sub + " " + s + " ";
}
return par + per + sub;
}
为此:
public String toString() {
return this.name + ":(Permission: " + permissions + ",Subjects: " + subjects + ",Parents: " +
parents.stream().map(role->role.name).collect(Collectors.toList()) + ")";
}