Minor change, extracted some commonly used code
This commit is contained in:
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.eclipse.moquette.spi.impl.security;
|
||||
|
||||
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission.READWRITE;
|
||||
|
||||
/**
|
||||
* Carries the read/write authorization to topics for the users.
|
||||
*
|
||||
@@ -40,6 +42,10 @@ public class Authorization {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
public boolean grant(Permission desiredPermission) {
|
||||
return permission == desiredPermission || permission == READWRITE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
||||
+17
-13
@@ -122,18 +122,14 @@ class AuthorizationsCollector implements IAuthorizator {
|
||||
}
|
||||
|
||||
private boolean canDoOperation(String topic, Authorization.Permission permission, String username, String client) {
|
||||
for (Authorization auth : m_globalAuthorizations) {
|
||||
if (auth.permission == permission || auth.permission == READWRITE) {
|
||||
if (SubscriptionsStore.matchTopics(topic, auth.topic)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (matchACL(m_globalAuthorizations, topic, permission)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isNotEmpty(client) || isNotEmpty(username)) {
|
||||
for (Authorization auth : m_patternAuthorizations) {
|
||||
String substitutedTopic = auth.topic.replace("%c", client).replace("%u", username);
|
||||
if (auth.permission == permission || auth.permission == READWRITE) {
|
||||
if (auth.grant(permission)) {
|
||||
if (SubscriptionsStore.matchTopics(topic, substitutedTopic)) {
|
||||
return true;
|
||||
}
|
||||
@@ -143,12 +139,20 @@ class AuthorizationsCollector implements IAuthorizator {
|
||||
|
||||
if (isNotEmpty(username)) {
|
||||
if (m_userAuthorizations.containsKey(username)) {
|
||||
for (Authorization auth : m_userAuthorizations.get(username)) {
|
||||
if (auth.permission == permission || auth.permission == READWRITE) {
|
||||
if (SubscriptionsStore.matchTopics(topic, auth.topic)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
List<Authorization> auths = m_userAuthorizations.get(username);
|
||||
if (matchACL(auths, topic, permission)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean matchACL(List<Authorization> auths, String topic, Authorization.Permission permission) {
|
||||
for (Authorization auth : auths) {
|
||||
if (auth.grant(permission)) {
|
||||
if (SubscriptionsStore.matchTopics(topic, auth.topic)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user