diff --git a/hsweb-authorization/hsweb-authorization-api/pom.xml b/hsweb-authorization/hsweb-authorization-api/pom.xml index 756f38c9c..9f66582c8 100644 --- a/hsweb-authorization/hsweb-authorization-api/pom.xml +++ b/hsweb-authorization/hsweb-authorization-api/pom.xml @@ -16,6 +16,15 @@ hsweb-boost-aop ${project.version} + + com.alibaba + fastjson + + + org.springframework.boot + spring-boot-starter + true + javax.servlet servlet-api diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/Permission.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/Permission.java index b25c5d175..f23ca20a1 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/Permission.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/Permission.java @@ -91,5 +91,5 @@ public interface Permission extends Serializable { * @return 用户对此权限持有的数据权限信息, 用于数据级别的控制 * @see DataAccessConfig */ - Set getDataAccessConfigs(); + Set getDataAccesses(); } diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/access/FieldAccessConfig.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/access/FieldAccessConfig.java index 474391279..98d388a59 100644 --- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/access/FieldAccessConfig.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/access/FieldAccessConfig.java @@ -28,7 +28,7 @@ public interface FieldAccessConfig extends Serializable { } enum Type { - //目前之支持 deny + //目前仅支持 deny DENY } } diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/AuthenticationBuilder.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/AuthenticationBuilder.java new file mode 100644 index 000000000..3f53d40d8 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/AuthenticationBuilder.java @@ -0,0 +1,55 @@ +/* + * Copyright 2016 http://www.hswebframework.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.hswebframework.web.authorization.builder; + +import org.hswebframework.web.authorization.Authentication; +import org.hswebframework.web.authorization.Permission; +import org.hswebframework.web.authorization.Role; +import org.hswebframework.web.authorization.User; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +public interface AuthenticationBuilder extends Serializable { + + AuthenticationBuilder user(User user); + + AuthenticationBuilder user(String user); + + AuthenticationBuilder user(Map user); + + + AuthenticationBuilder role(List role); + + AuthenticationBuilder role(String role); + + + AuthenticationBuilder permission(List permission); + + AuthenticationBuilder permission(String permission); + + AuthenticationBuilder attributes(String attributes); + + AuthenticationBuilder attributes(Map permission); + + AuthenticationBuilder json(String json); + + Authentication build(); + +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/AuthenticationBuilderFactory.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/AuthenticationBuilderFactory.java new file mode 100644 index 000000000..7c2616d1f --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/AuthenticationBuilderFactory.java @@ -0,0 +1,10 @@ +package org.hswebframework.web.authorization.builder; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public interface AuthenticationBuilderFactory { + AuthenticationBuilder create(); +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/DataAccessConfigBuilder.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/DataAccessConfigBuilder.java new file mode 100644 index 000000000..c0733e5f5 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/DataAccessConfigBuilder.java @@ -0,0 +1,13 @@ +package org.hswebframework.web.authorization.builder; + +import org.hswebframework.web.authorization.access.DataAccessConfig; + +/** + * + * @author zhouhao + */ +public interface DataAccessConfigBuilder { + DataAccessConfigBuilder fromJson(String json); + + DataAccessConfig build(); +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/DataAccessConfigBuilderFactory.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/DataAccessConfigBuilderFactory.java new file mode 100644 index 000000000..0caaf925c --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/DataAccessConfigBuilderFactory.java @@ -0,0 +1,10 @@ +package org.hswebframework.web.authorization.builder; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public interface DataAccessConfigBuilderFactory { + DataAccessConfigBuilder create(); +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/FieldAccessConfigBuilder.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/FieldAccessConfigBuilder.java new file mode 100644 index 000000000..23db2afc9 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/FieldAccessConfigBuilder.java @@ -0,0 +1,14 @@ +package org.hswebframework.web.authorization.builder; + +import org.hswebframework.web.authorization.access.FieldAccessConfig; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public interface FieldAccessConfigBuilder { + FieldAccessConfigBuilder fromJson(String json); + + FieldAccessConfig build(); +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/FieldAccessConfigBuilderFactory.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/FieldAccessConfigBuilderFactory.java new file mode 100644 index 000000000..f399daa3e --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/builder/FieldAccessConfigBuilderFactory.java @@ -0,0 +1,10 @@ +package org.hswebframework.web.authorization.builder; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public interface FieldAccessConfigBuilderFactory { + FieldAccessConfigBuilder create(); +} diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/AbstractDataAccess.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/AbstractDataAccess.java similarity index 84% rename from hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/AbstractDataAccess.java rename to hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/AbstractDataAccess.java index ee86d8d17..e00782560 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/AbstractDataAccess.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/AbstractDataAccess.java @@ -1,4 +1,4 @@ -package org.hswebframework.web.service.authorization.simple.access; +package org.hswebframework.web.authorization.simple; import org.hswebframework.web.authorization.access.DataAccessConfig; diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/AuthorizationAutoConfiguration.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/AuthorizationAutoConfiguration.java new file mode 100644 index 000000000..1ea1ed2fe --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/AuthorizationAutoConfiguration.java @@ -0,0 +1,50 @@ +package org.hswebframework.web.authorization.simple; + +import org.hswebframework.web.authorization.builder.AuthenticationBuilderFactory; +import org.hswebframework.web.authorization.builder.DataAccessConfigBuilderFactory; +import org.hswebframework.web.authorization.builder.FieldAccessConfigBuilderFactory; +import org.hswebframework.web.authorization.simple.builder.DataAccessConfigBuilderConvert; +import org.hswebframework.web.authorization.simple.builder.SimpleAuthenticationBuilderFactory; +import org.hswebframework.web.authorization.simple.builder.SimpleDataAccessConfigBuilderFactory; +import org.hswebframework.web.authorization.simple.builder.SimpleFieldAccessConfigBuilderFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.List; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +@Configuration +public class AuthorizationAutoConfiguration { + + @Autowired(required = false) + private List dataAccessConfigBuilderConverts; + + @Bean + @ConditionalOnMissingBean(FieldAccessConfigBuilderFactory.class) + public FieldAccessConfigBuilderFactory fieldAccessConfigBuilderFactory() { + return new SimpleFieldAccessConfigBuilderFactory(); + } + + @Bean + @ConditionalOnMissingBean(DataAccessConfigBuilderFactory.class) + public DataAccessConfigBuilderFactory dataAccessConfigBuilderFactory() { + SimpleDataAccessConfigBuilderFactory factory = new SimpleDataAccessConfigBuilderFactory(); + if (null != dataAccessConfigBuilderConverts) { + dataAccessConfigBuilderConverts.forEach(factory::addConvert); + } + return factory; + } + + @Bean + @ConditionalOnMissingBean(AuthenticationBuilderFactory.class) + public AuthenticationBuilderFactory authenticationBuilderFactory(DataAccessConfigBuilderFactory dataAccessConfigBuilderFactory + , FieldAccessConfigBuilderFactory fieldAccessConfigBuilderFactory) { + return new SimpleAuthenticationBuilderFactory(fieldAccessConfigBuilderFactory, dataAccessConfigBuilderFactory); + } +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleAuthentication.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleAuthentication.java new file mode 100644 index 000000000..838aa0588 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleAuthentication.java @@ -0,0 +1,88 @@ +/* + * Copyright 2016 http://www.hswebframework.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.hswebframework.web.authorization.simple; + +import org.hswebframework.web.authorization.*; + +import java.io.Serializable; +import java.util.*; + +public class SimpleAuthentication implements Authentication { + + private User user; + + private List roles; + + private List permissions; + + private Map attributes = new HashMap<>(); + + @Override + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + public void setPermissions(List permissions) { + this.permissions = permissions; + } + + @Override + public List getRoles() { + return new ArrayList<>(roles); + } + + @Override + public List getPermissions() { + return new ArrayList<>(permissions); + } + + @Override + @SuppressWarnings("unchecked") + public Optional getAttribute(String name) { + return Optional.ofNullable((T) attributes.get(name)); + } + + @Override + public void setAttribute(String name, Serializable object) { + attributes.put(name, object); + } + + @Override + public void setAttributes(Map attributes) { + this.attributes.putAll(attributes); + } + + @Override + @SuppressWarnings("unchecked") + public T removeAttributes(String name) { + return (T) attributes.remove(name); + } + + @Override + public Map getAttributes() { + return attributes; + } +} diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleCustomDataAccess.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleCustomDataAccess.java similarity index 94% rename from hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleCustomDataAccess.java rename to hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleCustomDataAccess.java index 1c453efd5..f19dd5e0c 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleCustomDataAccess.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleCustomDataAccess.java @@ -1,4 +1,4 @@ -package org.hswebframework.web.service.authorization.simple.access; +package org.hswebframework.web.authorization.simple; import org.hswebframework.web.authorization.access.CustomDataAccess; import org.hswebframework.web.authorization.access.DataAccessController; diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleFieldAccess.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleFieldAccess.java new file mode 100644 index 000000000..2f7ddfafb --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleFieldAccess.java @@ -0,0 +1,37 @@ +package org.hswebframework.web.authorization.simple; + +import org.hswebframework.web.authorization.access.FieldAccessConfig; + +import java.util.HashSet; +import java.util.Set; + +public class SimpleFieldAccess implements FieldAccessConfig { + private String field; + private Set actions; + + public SimpleFieldAccess() { + } + + public SimpleFieldAccess(String field, Set actions) { + this.field = field; + this.actions = actions; + } + + @Override + public String getField() { + return field; + } + + @Override + public Set getActions() { + return new HashSet<>(actions); + } + + public void setField(String field) { + this.field = field; + } + + public void setActions(Set actions) { + this.actions = actions; + } +} diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleOwnCreatedDataAccess.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleOwnCreatedDataAccess.java similarity index 55% rename from hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleOwnCreatedDataAccess.java rename to hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleOwnCreatedDataAccess.java index f74ebbeb2..084d9df52 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleOwnCreatedDataAccess.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleOwnCreatedDataAccess.java @@ -1,4 +1,4 @@ -package org.hswebframework.web.service.authorization.simple.access; +package org.hswebframework.web.authorization.simple; import org.hswebframework.web.authorization.access.OwnCreatedDataAccessConfig; @@ -8,4 +8,11 @@ import org.hswebframework.web.authorization.access.OwnCreatedDataAccessConfig; * @author zhouhao */ public class SimpleOwnCreatedDataAccess extends AbstractDataAccess implements OwnCreatedDataAccessConfig { + + public SimpleOwnCreatedDataAccess() { + } + + public SimpleOwnCreatedDataAccess(String action) { + setAction(action); + } } diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimplePermission.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimplePermission.java new file mode 100644 index 000000000..c9e067939 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimplePermission.java @@ -0,0 +1,67 @@ +package org.hswebframework.web.authorization.simple; + +import org.hswebframework.web.authorization.Permission; +import org.hswebframework.web.authorization.access.DataAccessConfig; +import org.hswebframework.web.authorization.access.FieldAccessConfig; + +import java.util.Set; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public class SimplePermission implements Permission { + + private String id; + + private Set actions; + + private Set fieldAccesses; + + private Set dataAccesses; + + public SimplePermission() { + } + + public SimplePermission(String id, Set actions) { + this.id = id; + this.actions = actions; + } + + @Override + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Override + public Set getActions() { + return actions; + } + + public void setActions(Set actions) { + this.actions = actions; + } + + @Override + public Set getFieldAccesses() { + return fieldAccesses; + } + + public void setFieldAccesses(Set fieldAccesses) { + this.fieldAccesses = fieldAccesses; + } + + @Override + public Set getDataAccesses() { + return dataAccesses; + } + + public void setDataAccesses(Set dataAccesses) { + this.dataAccesses = dataAccesses; + } +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleRole.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleRole.java new file mode 100644 index 000000000..fe71f4307 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleRole.java @@ -0,0 +1,40 @@ +package org.hswebframework.web.authorization.simple; + +import org.hswebframework.web.authorization.Role; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public class SimpleRole implements Role { + private String id; + + private String name; + + public SimpleRole() { + } + + public SimpleRole(String id, String name) { + this.id = id; + this.name = name; + } + + @Override + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Override + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleScriptDataAccess.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleScriptDataAccess.java similarity index 92% rename from hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleScriptDataAccess.java rename to hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleScriptDataAccess.java index 47cc4347c..d09fdfa37 100644 --- a/hsweb-system/hsweb-system-authorization/hsweb-system-authorization-service/hsweb-system-authorization-service-simple/src/main/java/org/hswebframework/web/service/authorization/simple/access/SimpleScriptDataAccess.java +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleScriptDataAccess.java @@ -1,4 +1,4 @@ -package org.hswebframework.web.service.authorization.simple.access; +package org.hswebframework.web.authorization.simple; import org.hswebframework.web.authorization.access.ScriptDataAccessConfig; diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleUser.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleUser.java new file mode 100644 index 000000000..fcf55df78 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/SimpleUser.java @@ -0,0 +1,50 @@ +package org.hswebframework.web.authorization.simple; + +import org.hswebframework.web.authorization.User; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public class SimpleUser implements User { + private String id; + private String username; + private String name; + + public SimpleUser() { + } + + public SimpleUser(String id, String username, String name) { + this.id = id; + this.username = username; + this.name = name; + } + + @Override + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Override + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + @Override + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/DataAccessConfigBuilderConvert.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/DataAccessConfigBuilderConvert.java new file mode 100644 index 000000000..5b6e53769 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/DataAccessConfigBuilderConvert.java @@ -0,0 +1,13 @@ +package org.hswebframework.web.authorization.simple.builder; + +import org.hswebframework.web.authorization.access.DataAccessConfig; + +/** + * @author zhouhao + */ +public interface DataAccessConfigBuilderConvert { + + boolean isSupport(String type, String action, String config); + + DataAccessConfig convert(String type, String action, String config); +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleAuthenticationBuilder.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleAuthenticationBuilder.java new file mode 100644 index 000000000..b54716430 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleAuthenticationBuilder.java @@ -0,0 +1,131 @@ +package org.hswebframework.web.authorization.simple.builder; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.hswebframework.web.authorization.Authentication; +import org.hswebframework.web.authorization.Permission; +import org.hswebframework.web.authorization.Role; +import org.hswebframework.web.authorization.User; +import org.hswebframework.web.authorization.builder.AuthenticationBuilder; +import org.hswebframework.web.authorization.builder.DataAccessConfigBuilderFactory; +import org.hswebframework.web.authorization.builder.FieldAccessConfigBuilderFactory; +import org.hswebframework.web.authorization.simple.SimpleAuthentication; +import org.hswebframework.web.authorization.simple.SimplePermission; +import org.hswebframework.web.authorization.simple.SimpleRole; +import org.hswebframework.web.authorization.simple.SimpleUser; + +import java.io.Serializable; +import java.util.*; +import java.util.stream.Collectors; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public class SimpleAuthenticationBuilder implements AuthenticationBuilder { + private SimpleAuthentication authentication = new SimpleAuthentication(); + + private FieldAccessConfigBuilderFactory fieldBuilderFactory; + private DataAccessConfigBuilderFactory dataBuilderFactory; + + public SimpleAuthenticationBuilder(FieldAccessConfigBuilderFactory fieldBuilderFactory, DataAccessConfigBuilderFactory dataBuilderFactory) { + this.fieldBuilderFactory = fieldBuilderFactory; + this.dataBuilderFactory = dataBuilderFactory; + } + + public void setFieldBuilderFactory(FieldAccessConfigBuilderFactory fieldBuilderFactory) { + this.fieldBuilderFactory = fieldBuilderFactory; + } + + public void setDataBuilderFactory(DataAccessConfigBuilderFactory dataBuilderFactory) { + this.dataBuilderFactory = dataBuilderFactory; + } + + @Override + public AuthenticationBuilder user(User user) { + Objects.requireNonNull(user); + authentication.setUser(user); + return this; + } + + @Override + public AuthenticationBuilder user(String user) { + return user(JSON.parseObject(user, SimpleUser.class)); + } + + @Override + public AuthenticationBuilder user(Map user) { + Objects.requireNonNull(user.get("id")); + user(new SimpleUser(user.get("id"), user.get("username"), user.get("name"))); + return this; + } + + @Override + public AuthenticationBuilder role(List role) { + authentication.setRoles(role); + return this; + } + + @Override + @SuppressWarnings("unchecked") + public AuthenticationBuilder role(String role) { + return role((List) JSON.parseArray(role, SimpleRole.class)); + } + + @Override + public AuthenticationBuilder permission(List permission) { + authentication.setPermissions(permission); + return this; + } + + @Override + public AuthenticationBuilder permission(String permissionJson) { + JSONArray jsonArray = JSON.parseArray(permissionJson); + List permissions = new ArrayList<>(); + for (int i = 0; i < jsonArray.size(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(0); + SimplePermission permission = new SimplePermission(); + permission.setId(jsonObject.getString("id")); + permission.setActions(new HashSet<>(jsonObject.getJSONArray("actions").toJavaList(String.class))); + permission.setFieldAccesses(jsonObject.getJSONArray("fieldAccesses").stream().map(JSONObject.class::cast) + .map(fieldJson -> fieldBuilderFactory.create().fromJson(fieldJson.toJSONString()).build()) + .collect(Collectors.toSet())); + + permission.setDataAccesses(jsonObject.getJSONArray("dataAccesses").stream().map(JSONObject.class::cast) + .map(dataJson -> dataBuilderFactory.create().fromJson(dataJson.toJSONString()).build()) + .collect(Collectors.toSet())); + permissions.add(permission); + } + + authentication.setPermissions(permissions); + return this; + } + + @Override + public AuthenticationBuilder attributes(String attributes) { + authentication.setAttributes(JSON.>parseObject(attributes, Map.class)); + return this; + } + + @Override + public AuthenticationBuilder attributes(Map permission) { + authentication.setAttributes(permission); + return this; + } + + @Override + public AuthenticationBuilder json(String json) { + JSONObject jsonObject = JSON.parseObject(json); + user(jsonObject.getObject("user", SimpleUser.class)); + role(jsonObject.getJSONArray("roles").toJSONString()); + permission(jsonObject.getJSONArray("permissions").toJSONString()); + return this; + } + + @Override + public Authentication build() { + return authentication; + } +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleAuthenticationBuilderFactory.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleAuthenticationBuilderFactory.java new file mode 100644 index 000000000..58c5668e9 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleAuthenticationBuilderFactory.java @@ -0,0 +1,27 @@ +package org.hswebframework.web.authorization.simple.builder; + +import org.hswebframework.web.authorization.builder.AuthenticationBuilder; +import org.hswebframework.web.authorization.builder.AuthenticationBuilderFactory; +import org.hswebframework.web.authorization.builder.DataAccessConfigBuilderFactory; +import org.hswebframework.web.authorization.builder.FieldAccessConfigBuilderFactory; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public class SimpleAuthenticationBuilderFactory implements AuthenticationBuilderFactory { + private FieldAccessConfigBuilderFactory fieldBuilderFactory; + + private DataAccessConfigBuilderFactory dataBuilderFactory; + + public SimpleAuthenticationBuilderFactory(FieldAccessConfigBuilderFactory fieldBuilderFactory, DataAccessConfigBuilderFactory dataBuilderFactory) { + this.fieldBuilderFactory = fieldBuilderFactory; + this.dataBuilderFactory = dataBuilderFactory; + } + + @Override + public AuthenticationBuilder create() { + return new SimpleAuthenticationBuilder(fieldBuilderFactory, dataBuilderFactory); + } +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleDataAccessConfigBuilder.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleDataAccessConfigBuilder.java new file mode 100644 index 000000000..7b1a8bbf1 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleDataAccessConfigBuilder.java @@ -0,0 +1,50 @@ +package org.hswebframework.web.authorization.simple.builder; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import org.hswebframework.web.authorization.access.DataAccessConfig; +import org.hswebframework.web.authorization.builder.DataAccessConfigBuilder; +import org.hswebframework.web.authorization.simple.SimpleCustomDataAccess; +import org.hswebframework.web.authorization.simple.SimpleOwnCreatedDataAccess; + +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +/** + * @author zhouhao + */ +public class SimpleDataAccessConfigBuilder implements DataAccessConfigBuilder { + private String json; + + private List converts; + + public SimpleDataAccessConfigBuilder(List converts) { + Objects.requireNonNull(converts); + this.converts = converts; + } + + @Override + public DataAccessConfigBuilder fromJson(String json) { + this.json = json; + return this; + } + + @Override + public DataAccessConfig build() { + Objects.requireNonNull(json); + JSONObject jsonObject = JSON.parseObject(json); + + String type = jsonObject.getString("type"); + String action = jsonObject.getString("action"); + String config = jsonObject.getString("config"); + + Objects.requireNonNull(type); + Objects.requireNonNull(action); + + + return converts.stream().filter(convert -> convert.isSupport(type, action, config)) + .findAny().map(convert -> convert.convert(type, action, config)) + .orElse(null); + } +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleDataAccessConfigBuilderFactory.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleDataAccessConfigBuilderFactory.java new file mode 100644 index 000000000..b6011dfe8 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleDataAccessConfigBuilderFactory.java @@ -0,0 +1,73 @@ +package org.hswebframework.web.authorization.simple.builder; + +import com.alibaba.fastjson.JSON; +import org.hswebframework.web.authorization.access.DataAccessConfig; +import org.hswebframework.web.authorization.builder.DataAccessConfigBuilder; +import org.hswebframework.web.authorization.builder.DataAccessConfigBuilderFactory; +import org.hswebframework.web.authorization.simple.SimpleCustomDataAccess; +import org.hswebframework.web.authorization.simple.SimpleOwnCreatedDataAccess; + +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public class SimpleDataAccessConfigBuilderFactory implements DataAccessConfigBuilderFactory { + private List converts = new LinkedList<>(); + + public SimpleDataAccessConfigBuilderFactory addConvert(DataAccessConfigBuilderConvert configBuilderConvert) { + Objects.requireNonNull(configBuilderConvert); + converts.add(configBuilderConvert); + return this; + } + + public SimpleDataAccessConfigBuilderFactory() { + converts.add(new DataAccessConfigBuilderConvert() { + @Override + public boolean isSupport(String type, String action, String config) { + return DataAccessConfig.DefaultType.OWN_CREATED.equals(type); + } + + @Override + public DataAccessConfig convert(String type, String action, String config) { + return new SimpleOwnCreatedDataAccess(action); + } + }); + + converts.add(new DataAccessConfigBuilderConvert() { + @Override + public boolean isSupport(String type, String action, String config) { + return DataAccessConfig.DefaultType.SCRIPT.equals(type); + } + + @Override + public DataAccessConfig convert(String type, String action, String config) { + SimpleOwnCreatedDataAccess access = JSON.parseObject(config, SimpleOwnCreatedDataAccess.class); + access.setAction(config); + return access; + } + }); + converts.add(new DataAccessConfigBuilderConvert() { + @Override + public boolean isSupport(String type, String action, String config) { + return DataAccessConfig.DefaultType.CUSTOM.equals(type); + } + + @Override + public DataAccessConfig convert(String type, String action, String config) { + SimpleCustomDataAccess access = new SimpleCustomDataAccess(config); + access.setAction(action); + return access; + } + }); + } + + @Override + public DataAccessConfigBuilder create() { + return new SimpleDataAccessConfigBuilder(converts); + } +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleFieldAccessConfigBuilder.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleFieldAccessConfigBuilder.java new file mode 100644 index 000000000..a99f2830f --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleFieldAccessConfigBuilder.java @@ -0,0 +1,30 @@ +package org.hswebframework.web.authorization.simple.builder; + +import com.alibaba.fastjson.JSON; +import org.hswebframework.web.authorization.access.FieldAccessConfig; +import org.hswebframework.web.authorization.builder.FieldAccessConfigBuilder; +import org.hswebframework.web.authorization.simple.SimpleFieldAccess; + +import java.util.Objects; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public class SimpleFieldAccessConfigBuilder implements FieldAccessConfigBuilder { + + private String json; + + @Override + public FieldAccessConfigBuilder fromJson(String json) { + this.json = json; + return this; + } + + @Override + public FieldAccessConfig build() { + Objects.requireNonNull(json); + return JSON.parseObject(json, SimpleFieldAccess.class); + } +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleFieldAccessConfigBuilderFactory.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleFieldAccessConfigBuilderFactory.java new file mode 100644 index 000000000..33b4174e1 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/simple/builder/SimpleFieldAccessConfigBuilderFactory.java @@ -0,0 +1,16 @@ +package org.hswebframework.web.authorization.simple.builder; + +import org.hswebframework.web.authorization.builder.FieldAccessConfigBuilder; +import org.hswebframework.web.authorization.builder.FieldAccessConfigBuilderFactory; + +/** + * TODO 完成注释 + * + * @author zhouhao + */ +public class SimpleFieldAccessConfigBuilderFactory implements FieldAccessConfigBuilderFactory { + @Override + public FieldAccessConfigBuilder create() { + return new SimpleFieldAccessConfigBuilder(); + } +} diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/resources/META-INF/spring.factories b/hsweb-authorization/hsweb-authorization-api/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..4b1c942a5 --- /dev/null +++ b/hsweb-authorization/hsweb-authorization-api/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +# Auto Configure +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.hswebframework.web.authorization.simple.AuthorizationAutoConfiguration \ No newline at end of file