Skip to content

Commit

Permalink
Added improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ark2307 committed Nov 26, 2024
1 parent 5f9ad21 commit 8086af7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ public String createNewAccount() {
}

public static User initializeAccount(String email, int newAccountId, String newAccountName, boolean isNew, RBAC.Role role) {
UsersDao.addAccount(email, newAccountId, newAccountName);
User user = UsersDao.instance.findOne(eq(User.LOGIN, email));
User user = UsersDao.addAccount(email, newAccountId, newAccountName);
RBACDao.instance.insertOne(new RBAC(user.getId(), role, newAccountId));
Context.accountId.set(newAccountId);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public static void executeMeta1(Utility utility, User user, HttpServletRequest r
if(currAccount != null && !currAccount.getTimezone().isEmpty()){
timeZone = currAccount.getTimezone();
}
String dashboardVersion = accountSettings.getDashboardVersion();
String dashboardVersion = "";
if(accountSettings != null){
dashboardVersion = accountSettings.getDashboardVersion();
}
String[] versions = dashboardVersion.split(" - ");
User userFromDB = UsersDao.instance.findOne(Filters.eq(Constants.ID, user.getId()));
RBAC.Role userRole = RBACDao.getCurrentRoleForUser(user.getId(), Context.accountId.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public class SignupAction implements Action, ServletResponseAware, ServletReques
public static final String BUSINESS_EMAIL_REQUIRED_ERROR = "BUSINESS_EMAIL_REQUIRED";
public static final String ERROR_STR = "error";
public static final String ERROR_DESCRIPTION = "error_description";
private static final Logger logger = LoggerFactory.getLogger(ProfileAction.class);
private static final Logger logger = LoggerFactory.getLogger(SignupAction.class);
private static final LoggerMaker loggerMaker = new LoggerMaker(SignupAction.class);

public String getCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,10 @@ public static void executePIISourceFetch() {
(existingCDT != null && existingCDT.getDataTypePriority() != null)
&& (existingCDT.getCategoriesList() != null && !existingCDT.getCategoriesList().isEmpty());

boolean userHasChangedCondition = existingCDT.getUserModifiedTimestamp() > 0;
boolean userHasChangedCondition = false;
if(existingCDT != null && existingCDT.getUserModifiedTimestamp() > 0){
userHasChangedCondition = true;
}

if (userHasChangedCondition || hasNotChangedCondition) {
continue;
Expand Down Expand Up @@ -2451,26 +2454,24 @@ public static void insertPiiSources(){

static boolean executedOnce = false;

private final static int REFRESH_INTERVAL = 60 * 1; // 1 minute
private final static int REFRESH_INTERVAL = 60 * 15; // 15 minute

public static Organization fetchAndSaveFeatureWiseAllowed(Organization organization) {


int lastFeatureMapUpdate = organization.getLastFeatureMapUpdate();
if((lastFeatureMapUpdate + REFRESH_INTERVAL) >= Context.now()){
return organization;
}
HashMap<String, FeatureAccess> featureWiseAllowed = new HashMap<>();

try {
int gracePeriod = organization.getGracePeriod();
String hotjarSiteId = organization.getHotjarSiteId();
String organizationId = organization.getId();

int lastFeatureMapUpdate = organization.getLastFeatureMapUpdate();

/*
* This ensures, we don't fetch feature wise allowed from akto too often.
* This helps the dashboard to be more responsive.
*/
if(lastFeatureMapUpdate + REFRESH_INTERVAL > Context.now()){
return organization;
}

HashMap<String, FeatureAccess> initialFeatureWiseAllowed = organization.getFeatureWiseAllowed();
if (initialFeatureWiseAllowed == null) {
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/src/main/java/com/akto/utils/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public static String generateAccessToken(String username, String signedUp) throw
claims,
"Akto",
"login",
Calendar.MINUTE,
15
Calendar.HOUR,
2
);

}
Expand Down
13 changes: 11 additions & 2 deletions libs/dao/src/main/java/com/akto/dao/UsersDao.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.akto.dao;

import com.akto.dao.context.Context;
import com.akto.dto.*;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.FindOneAndUpdateOptions;
import com.mongodb.client.model.ReturnDocument;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.model.Updates;

import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -40,9 +44,14 @@ public static User addUser(String login, String name, String password, boolean e
return null;
}

public static void addAccount(String login, int accountId, String name) {
public static User addAccount(String login, int accountId, String name) {
BasicDBObject setQ = new BasicDBObject(User.ACCOUNTS + "." + accountId,new UserAccountEntry(accountId, name));
UsersDao.instance.getMCollection().updateOne(eq(User.LOGIN, login), new BasicDBObject(SET, setQ));

User tempUser = UsersDao.instance.getMCollection().findOneAndUpdate(
eq(User.LOGIN, login), new BasicDBObject(SET, setQ),
new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)
);
return tempUser;
}

public static void addNewAccount(String login, Account account){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private String executeGraphQL(String query, String vars) throws IllegalStateExce
if (stiggConfig == null) {
throw new IllegalStateException("Stigg config is not initialised");
}
int timeNow = Context.now();
String requestBody = String.format("{\"query\":\"%s\",\"variables\":%s}", query, vars);

// Set the GraphQL endpoint URL
Expand All @@ -67,12 +68,18 @@ private String executeGraphQL(String query, String vars) throws IllegalStateExce

// Execute the request and get the response
try (Response response = client.newCall(request).execute()) {
String[] queryTypes = query.split("(");
String queryString = queryTypes[0];

if (!response.isSuccessful()) {
throw new IOException("Unexpected response code: " + response);
}

String responseBodyStr = response.body().string();
loggerMaker.infoAndAddToDb("Time taken by stigg call for query: "+ queryString + " is: " + (Context.now() - timeNow));

timeNow = Context.now();
BasicDBObject responseBodyObj = BasicDBObject.parse(responseBodyStr);
loggerMaker.infoAndAddToDb("Time taken by parsing response for query: "+ queryString + " is: " + (Context.now() - timeNow));
return responseBodyObj.toJson();
} catch (Exception e) {
return new BasicDBObject("err", e.getMessage()).toJson();
Expand Down

0 comments on commit 8086af7

Please sign in to comment.