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