Skip to content

Commit

Permalink
more typesafe use of AccountNode
Browse files Browse the repository at this point in the history
  • Loading branch information
Clay-Ferguson committed Dec 15, 2024
1 parent 6bd92ca commit ba8a571
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
7 changes: 3 additions & 4 deletions src/main/java/quanta/mongo/MongoAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public List<String> getUsersSharedTo(SubNode node) {
name = PrincipalName.PUBLIC.s();
} //
else if (userNodeId != null) {
SubNode accountNode = svc_mongoRead.getNode(userNodeId);
AccountNode accountNode = svc_user.getAccountNode(userNodeId);
if (accountNode != null) {
name = accountNode.getStr(NodeProp.USER);
}
Expand Down Expand Up @@ -420,7 +420,7 @@ public boolean nodeAuth(SubNode node, String sessionUserNodeId, List<PrivilegeTy
}

/*
* todo-0: this appears to return "rd,wr" as a single string in the privileges.privilegeName,
* todo-1: this appears to return "rd,wr" as a single string in the privileges.privilegeName,
* whereas the SubNode itself I think stores them as separate strings in the AccessControl.prvs
* field. This is a bit confusing.
*/
Expand Down Expand Up @@ -453,8 +453,7 @@ public AccessControlInfo createAccessControlInfo(String principalId, String auth
}
// else we need the user name
else {
// todo-0: this can be AccountNode here?
SubNode principalNode = svc_mongoRead.getNodeAP(principalId);
AccountNode principalNode = svc_user.getAccountNodeAP(principalId);
if (principalNode == null) {
return null;
}
Expand Down
22 changes: 0 additions & 22 deletions src/main/java/quanta/mongo/MongoRead.java
Original file line number Diff line number Diff line change
Expand Up @@ -991,28 +991,6 @@ public SubNode getUserNodeByType(String userName, AccountNode userNode, String c
return node;
}

public SubNode getUserNodeByPropAP(String propName, String propVal, boolean caseSensitive) {
return svc_arun.run(() -> getUserNodeByProp(propName, propVal, caseSensitive));
}

public SubNode getUserNodeByProp(String propName, String propVal, boolean caseSensitive) {
if (StringUtils.isEmpty(propVal))
return null;
// Otherwise for ordinary users root is based off their username
Query q = new Query();
Criteria crit;
if (caseSensitive) {
crit = svc_mongoUtil.childrenCriteria(NodePath.USERS_PATH).and(SubNode.PROPS + "." + propName).is(propVal);
} else {
crit = svc_mongoUtil.childrenCriteria(NodePath.USERS_PATH).and(SubNode.PROPS + "." + propName)
.regex("^" + Pattern.quote(propVal) + "$", "i");
}

crit = svc_auth.addReadSecurity(crit);
q.addCriteria(crit);
return svc_ops.findOne(q);
}

/*
* Finds and returns the first node matching userName and type as direct child of 'node', or null if
* not existing. Caller can pass userNode if its available, or else userName will be used to look it
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/quanta/service/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import quanta.model.client.NodeProp;
import quanta.model.client.NodeType;
import quanta.mongo.MongoRepository;
import quanta.mongo.model.AccountNode;
import quanta.mongo.model.SubNode;
import quanta.util.LimitedInputStream;
import quanta.util.TL;
Expand Down Expand Up @@ -206,9 +207,9 @@ public String convertEmailToMarkdown(LimitedInputStream is) {
/**
* Sends an email notification to the user associated with 'toUserNode' (a person's account root
* node), telling them that 'fromUserName' has shared a node with them, and including a link to the
* shared node in the email.
* shared node in the email. (Not currently usee)
*/
public void sendEmailNotification(String fromUserName, SubNode toUserNode, SubNode node) {
public void sendEmailNotification(String fromUserName, AccountNode toUserNode, SubNode node) {
String email = toUserNode.getStr(NodeProp.EMAIL);
String toUserName = toUserNode.getStr(NodeProp.USER);
String nodeUrl = svc_snUtil.getFriendlyNodeUrl(node);
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/quanta/service/UserManagerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Random;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
Expand Down Expand Up @@ -423,7 +424,7 @@ public SignupResponse signup(SignupRequest req, boolean automated) {

// we disallow dupliate emails via this codepath, but by design we do allow them in the DB, and
// even all the 'test accounts' will normally have the same email address.
SubNode ownerNode = svc_mongoRead.getUserNodeByPropAP(NodeProp.EMAIL.s(), email, false);
AccountNode ownerNode = svc_user.getUserNodeByPropAP(NodeProp.EMAIL.s(), email, false);
if (ownerNode != null) {
res.setEmailError("Email already in use.");
res.setCode(HttpServletResponse.SC_EXPECTATION_FAILED);
Expand Down Expand Up @@ -540,7 +541,7 @@ public AddCreditResponse addCredit(String userId, BigDecimal amount) {

public Tran addCreditByEmail(String emailAdr, BigDecimal amount, Long timestamp) {
PgTranMgr.ensureTran();
SubNode ownerNode = svc_mongoRead.getUserNodeByPropAP(NodeProp.EMAIL.s(), emailAdr, false);
AccountNode ownerNode = svc_user.getUserNodeByPropAP(NodeProp.EMAIL.s(), emailAdr, false);
if (ownerNode != null) {
String userName = ownerNode.getStr(NodeProp.USER);
Tran tran = addCreditInternal(ownerNode.getIdStr(), amount, timestamp);
Expand Down Expand Up @@ -1247,6 +1248,28 @@ public SendFeedbackResponse cm_sendFeedback(SendFeedbackRequest req) {
});
}

public AccountNode getUserNodeByPropAP(String propName, String propVal, boolean caseSensitive) {
return svc_arun.run(() -> getUserNodeByProp(propName, propVal, caseSensitive));
}

public AccountNode getUserNodeByProp(String propName, String propVal, boolean caseSensitive) {
if (StringUtils.isEmpty(propVal))
return null;
// Otherwise for ordinary users root is based off their username
Query q = new Query();
Criteria crit;
if (caseSensitive) {
crit = svc_mongoUtil.childrenCriteria(NodePath.USERS_PATH).and(SubNode.PROPS + "." + propName).is(propVal);
} else {
crit = svc_mongoUtil.childrenCriteria(NodePath.USERS_PATH).and(SubNode.PROPS + "." + propName)
.regex("^" + Pattern.quote(propVal) + "$", "i");
}

crit = svc_auth.addReadSecurity(crit);
q.addCriteria(crit);
return svc_ops.findOne(q, AccountNode.class);
}

public AccountNode getAccountNode(String id) {
return svc_ops.findById(new ObjectId(id), AccountNode.class);
}
Expand Down

0 comments on commit ba8a571

Please sign in to comment.