Esempio n. 1
0
  @With({UserCredentialWrapFilter.class, ConnectToDBFilter.class})
  public static Result follow(String toFollowUsername) {

    String currentUsername = DbHelper.currentUsername();

    try {
      UserService.getOUserByUsername(currentUsername);
    } catch (Exception e) {
      return internalServerError(ExceptionUtils.getMessage(e));
    }
    try {
      ODocument followed = FriendShipService.follow(currentUsername, toFollowUsername);
      return created(prepareResponseToJson(followed));
    } catch (UserToFollowNotExistsException e) {
      return notFound(ExceptionUtils.getMessage(e));
    } catch (UserNotFoundException e) {
      return internalServerError(ExceptionUtils.getMessage(e));
    } catch (AlreadyFriendsException e) {
      return badRequest(ExceptionUtils.getMessage(e));
    } catch (SqlInjectionException e) {
      return badRequest(
          "The username "
              + toFollowUsername
              + " is not a valid username. HINT: check if it contains invalid character, the server has encountered a possible SQL Injection attack");
    } catch (IllegalArgumentException e) {
      return badRequest(ExceptionUtils.getMessage(e));
    } catch (Exception e) {
      return internalServerError(ExceptionUtils.getMessage(e));
    }
  }
Esempio n. 2
0
 /**
  * * Returns the people those the given user is following
  *
  * @param username
  * @return
  */
 @With({UserCredentialWrapFilter.class, ConnectToDBFilter.class, ExtractQueryParameters.class})
 public static Result following(String username) {
   if (StringUtils.isEmpty(username)) username = DbHelper.currentUsername();
   try {
     Context ctx = Http.Context.current.get();
     QueryParams criteria = (QueryParams) ctx.args.get(IQueryParametersKeys.QUERY_PARAMETERS);
     List<ODocument> following = FriendShipService.getFollowing(username, criteria);
     return ok(prepareResponseToJson(following));
   } catch (SqlInjectionException e) {
     return internalServerError(ExceptionUtils.getFullStackTrace(e));
   }
 }
Esempio n. 3
0
 public static void addUserToRole(String username, String role) {
   boolean admin = true;
   if (!DbHelper.currentUsername().equals(BBConfiguration.getBaasBoxAdminUsername())) {
     DbHelper.reconnectAsAdmin();
     admin = false;
   }
   String sqlAdd = "update ouser add roles = {TO_ROLE} where name = ?";
   ORole toRole = RoleDao.getRole(role);
   ORID toRID = toRole.getDocument().getRecord().getIdentity();
   sqlAdd = sqlAdd.replace("{TO_ROLE}", toRID.toString());
   GenericDao.getInstance().executeCommand(sqlAdd, new String[] {username});
   if (!admin) {
     DbHelper.reconnectAsAuthenticatedUser();
   }
 }
Esempio n. 4
0
 public static void removeUserFromRole(String username, String role) {
   boolean admin = false;
   if (!DbHelper.currentUsername().equals(BBConfiguration.getBaasBoxAdminUsername())) {
     DbHelper.reconnectAsAdmin();
     admin = true;
   }
   String sqlRemove =
       "update ouser remove roles = {FROM_ROLE} where roles contains {FROM_ROLE} and name = ?";
   ORole fromRole = RoleDao.getRole(role);
   ORID fromRID = fromRole.getDocument().getRecord().getIdentity();
   sqlRemove = sqlRemove.replace("{FROM_ROLE}", fromRID.toString());
   GenericDao.getInstance().executeCommand(sqlRemove, new String[] {username});
   if (admin) {
     DbHelper.reconnectAsAuthenticatedUser();
   }
 }
Esempio n. 5
0
  @With({UserCredentialWrapFilter.class, ConnectToDBFilter.class})
  public static Result unfollow(String toUnfollowUsername) {
    String currentUsername = DbHelper.currentUsername();

    try {
      boolean success = FriendShipService.unfollow(currentUsername, toUnfollowUsername);
      if (success) {
        return ok();
      } else {
        return notFound("User " + currentUsername + " is not a friend of " + toUnfollowUsername);
      }
    } catch (UserNotFoundException e) {
      return notFound(ExceptionUtils.getMessage(e));
    } catch (Exception e) {
      return internalServerError(ExceptionUtils.getMessage(e));
    }
  }
Esempio n. 6
0
 public ODocument createLink(String sourceId, String destId, String edgeName)
     throws DocumentNotFoundException {
   DbHelper.requestTransaction();
   OrientEdge edge = null;
   try {
     OrientVertex sourceVertex = StorageUtils.getNodeVertex(sourceId);
     OrientVertex destVertex = StorageUtils.getNodeVertex(destId);
     UUID token = UUID.randomUUID();
     edge = (OrientEdge) sourceVertex.addEdge(edgeName, destVertex);
     edge.getRecord().field(BaasBoxPrivateFields.ID.toString(), token.toString());
     edge.getRecord().field(BaasBoxPrivateFields.AUTHOR.toString(), DbHelper.currentUsername());
     edge.getRecord().field(BaasBoxPrivateFields.CREATION_DATE.toString(), new Date());
     edge.save();
     DbHelper.commitTransaction();
   } catch (DocumentNotFoundException e) {
     DbHelper.rollbackTransaction();
     throw e;
   }
   // edge.getGraph().commit();
   return edge.getRecord();
 }
Esempio n. 7
0
 /**
  * * Returns the followers of the current user
  *
  * @return
  */
 @With({UserCredentialWrapFilter.class, ConnectToDBFilter.class, ExtractQueryParameters.class})
 public static Result followers(boolean justCountThem, String username) {
   if (StringUtils.isEmpty(username)) username = DbHelper.currentUsername();
   Context ctx = Http.Context.current.get();
   QueryParams criteria = (QueryParams) ctx.args.get(IQueryParametersKeys.QUERY_PARAMETERS);
   List<ODocument> listOfFollowers = new ArrayList<ODocument>();
   long count = 0;
   try {
     if (justCountThem) count = FriendShipService.getCountFriendsOf(username, criteria);
     else listOfFollowers = FriendShipService.getFriendsOf(username, criteria);
   } catch (InvalidCriteriaException e) {
     return badRequest(ExceptionUtils.getMessage(e));
   } catch (SqlInjectionException e) {
     return badRequest(
         "The parameters you passed are incorrect. HINT: check if the querystring is correctly encoded");
   }
   if (justCountThem) {
     response().setContentType("application/json");
     return ok("{\"count\": " + count + " }");
   } else {
     String ret = prepareResponseToJson(listOfFollowers);
     return ok(ret);
   }
 }
Esempio n. 8
0
 public static void disableCurrentUser() throws UserNotFoundException {
   String username = DbHelper.currentUsername();
   disableUser(username);
 }