Beispiel #1
0
 public void send(String message, String username)
     throws PushNotInitializedException, UserNotFoundException, SqlInjectionException,
         InvalidRequestException, IOException, UnknownHostException {
   if (Logger.isDebugEnabled())
     Logger.debug("Try to send a message (" + message + ") to " + username);
   UserDao udao = UserDao.getInstance();
   ODocument user = udao.getByUserName(username);
   if (user == null) {
     if (Logger.isDebugEnabled()) Logger.debug("User " + username + " does not exist");
     throw new UserNotFoundException("User " + username + " does not exist");
   }
   ODocument userSystemProperties = user.field(UserDao.ATTRIBUTES_SYSTEM);
   if (Logger.isDebugEnabled()) Logger.debug("userSystemProperties: " + userSystemProperties);
   List<ODocument> loginInfos = userSystemProperties.field(UserDao.USER_LOGIN_INFO);
   if (Logger.isDebugEnabled()) Logger.debug("Sending to " + loginInfos.size() + " devices");
   for (ODocument loginInfo : loginInfos) {
     String pushToken = loginInfo.field(UserDao.USER_PUSH_TOKEN);
     String vendor = loginInfo.field(UserDao.USER_DEVICE_OS);
     if (Logger.isDebugEnabled()) Logger.debug("push token: " + pushToken);
     if (Logger.isDebugEnabled()) Logger.debug("vendor: " + vendor);
     if (!StringUtils.isEmpty(vendor) && !StringUtils.isEmpty(pushToken)) {
       VendorOS vos = VendorOS.getVendorOs(vendor);
       if (Logger.isDebugEnabled()) Logger.debug("vos: " + vos);
       if (vos != null) {
         IPushServer pushServer = Factory.getIstance(vos);
         pushServer.setConfiguration(getPushParameters());
         pushServer.send(message, pushToken);
       } // vos!=null
     } // (!StringUtils.isEmpty(vendor) && !StringUtils.isEmpty(deviceId)
   } // for (ODocument loginInfo : loginInfos)
 } // send
Beispiel #2
0
 public static void changePassword(String username, String newPassword)
     throws SqlInjectionException, UserNotFoundException {
   ODatabaseRecordTx db = DbHelper.getConnection();
   db = DbHelper.reconnectAsAdmin();
   UserDao udao = UserDao.getInstance();
   ODocument user = udao.getByUserName(username);
   if (user == null) {
     if (Logger.isDebugEnabled()) Logger.debug("User " + username + " does not exist");
     throw new UserNotFoundException("User " + username + " does not exist");
   }
   db.getMetadata().getSecurity().getUser(username).setPassword(newPassword).save();
 }
Beispiel #3
0
  public static ODocument updateProfile(
      String username,
      String role,
      JsonNode nonAppUserAttributes,
      JsonNode privateAttributes,
      JsonNode friendsAttributes,
      JsonNode appUsersAttributes)
      throws Exception {
    try {
      ORole newORole = RoleDao.getRole(role);
      if (newORole == null) throw new InvalidParameterException(role + " is not a role");
      if (!RoleService.isAssignable(newORole))
        throw new RoleIsNotAssignableException("Role " + role + " is not assignable");
      ORID newRole = newORole.getDocument().getIdentity();
      UserDao udao = UserDao.getInstance();
      ODocument profile = udao.getByUserName(username);
      if (profile == null) throw new InvalidParameterException(username + " is not a user");
      profile =
          updateProfile(
              profile,
              nonAppUserAttributes,
              privateAttributes,
              friendsAttributes,
              appUsersAttributes);

      Set<OIdentifiable> roles =
          (Set<OIdentifiable>) ((ODocument) profile.field("user")).field("roles");
      // extracts the role skipping the friends ones
      String oldRole = null;
      for (OIdentifiable r : roles) {
        oldRole = ((String) ((ODocument) r.getRecord()).field("name"));
        if (!oldRole.startsWith(RoleDao.FRIENDS_OF_ROLE)) {
          break;
        }
      }
      ORole oldORole = RoleDao.getRole(oldRole);
      // TODO: update role
      OUser ouser = DbHelper.getConnection().getMetadata().getSecurity().getUser(username);
      ouser.getRoles().remove(oldORole);
      ouser.addRole(newORole);
      ouser.save();
      profile.save();
      profile.reload();

      return profile;
    } catch (Exception e) {
      throw e;
    }
  } // updateProfile with role
Beispiel #4
0
 public static void resetUserPasswordFinalStep(String username, String newPassword)
     throws SqlInjectionException, ResetPasswordException {
   ODocument user = UserDao.getInstance().getByUserName(username);
   ODocument ouser = ((ODocument) user.field("user"));
   ouser.field("password", newPassword).save();
   ResetPwdDao.getInstance().setResetPasswordDone(username);
 }
Beispiel #5
0
 public static List<ODocument> getUserProfilebyUsernames(List<String> usernames)
     throws SqlInjectionException {
   return UserDao.getInstance().getByUsernames(usernames);
 }
Beispiel #6
0
 public static void enableUser(String username) throws UserNotFoundException {
   UserDao.getInstance().enableUser(username);
 }
Beispiel #7
0
 public static boolean exists(String username) {
   UserDao udao = UserDao.getInstance();
   return udao.existsUserName(username);
 }
Beispiel #8
0
  public static ODocument signUp(
      String username,
      String password,
      Date signupDate,
      String role,
      JsonNode nonAppUserAttributes,
      JsonNode privateAttributes,
      JsonNode friendsAttributes,
      JsonNode appUsersAttributes,
      boolean generated)
      throws OSerializationException, Exception {

    ODatabaseRecordTx db = DbHelper.getConnection();
    ODocument profile = null;
    UserDao dao = UserDao.getInstance();
    try {
      // because we have to create an OUser record and a User Object, we need a transaction

      DbHelper.requestTransaction();

      if (role == null) profile = dao.create(username, password);
      else profile = dao.create(username, password, role);

      ORID userRid = ((ODocument) profile.field("user")).getIdentity();
      ORole friendRole = RoleDao.createFriendRole(username);
      friendRole.getDocument().field(RoleService.FIELD_ASSIGNABLE, true);
      friendRole.getDocument().field(RoleService.FIELD_MODIFIABLE, false);
      friendRole.getDocument().field(RoleService.FIELD_INTERNAL, true);
      friendRole
          .getDocument()
          .field(RoleService.FIELD_DESCRIPTION, "These are friends of " + username);

      /*    these attributes are visible by:
       *    Anonymous users
       *    Registered user
       *    Friends
       *    User
       */

      // anonymous
      {
        ODocument attrObj = new ODocument(dao.USER_ATTRIBUTES_CLASS);
        try {
          if (nonAppUserAttributes != null) attrObj.fromJSON(nonAppUserAttributes.toString());
          else attrObj.fromJSON("{}");
        } catch (OSerializationException e) {
          throw new OSerializationException(
              dao.ATTRIBUTES_VISIBLE_BY_ANONYMOUS_USER + " is not a valid JSON object", e);
        }
        PermissionsHelper.grantRead(
            attrObj, RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString()));
        PermissionsHelper.grantRead(
            attrObj, RoleDao.getRole(DefaultRoles.ANONYMOUS_USER.toString()));
        PermissionsHelper.grantRead(attrObj, friendRole);
        PermissionsHelper.changeOwner(attrObj, userRid);
        profile.field(dao.ATTRIBUTES_VISIBLE_BY_ANONYMOUS_USER, attrObj);
        attrObj.save();
      }

      /*    these attributes are visible by:
       *    User
       */
      {
        ODocument attrObj = new ODocument(dao.USER_ATTRIBUTES_CLASS);
        try {
          if (privateAttributes != null) attrObj.fromJSON(privateAttributes.toString());
          else attrObj.fromJSON("{}");
        } catch (OSerializationException e) {
          throw new OSerializationException(
              dao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER + " is not a valid JSON object", e);
        }
        profile.field(dao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER, attrObj);
        PermissionsHelper.changeOwner(attrObj, userRid);
        attrObj.save();
      }

      /*    these attributes are visible by:
       *    Friends
       *    User
       */
      {
        ODocument attrObj = new ODocument(dao.USER_ATTRIBUTES_CLASS);
        try {
          if (friendsAttributes != null) attrObj.fromJSON(friendsAttributes.toString());
          else attrObj.fromJSON("{}");
        } catch (OSerializationException e) {
          throw new OSerializationException(
              dao.ATTRIBUTES_VISIBLE_BY_FRIENDS_USER + " is not a valid JSON object", e);
        }
        PermissionsHelper.grantRead(attrObj, friendRole);
        PermissionsHelper.changeOwner(attrObj, userRid);
        profile.field(dao.ATTRIBUTES_VISIBLE_BY_FRIENDS_USER, attrObj);
        attrObj.save();
      }

      /*    these attributes are visible by:
       *    Registered user
       *    Friends
       *    User
       */
      {
        ODocument attrObj = new ODocument(dao.USER_ATTRIBUTES_CLASS);
        try {
          if (appUsersAttributes != null) attrObj.fromJSON(appUsersAttributes.toString());
          else attrObj.fromJSON("{}");
        } catch (OSerializationException e) {
          throw new OSerializationException(
              dao.ATTRIBUTES_VISIBLE_BY_REGISTERED_USER + " is not a valid JSON object", e);
        }
        PermissionsHelper.grantRead(
            attrObj, RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString()));
        PermissionsHelper.changeOwner(attrObj, userRid);
        profile.field(dao.ATTRIBUTES_VISIBLE_BY_REGISTERED_USER, attrObj);
        attrObj.save();
      }

      ODocument attrObj = new ODocument(dao.USER_ATTRIBUTES_CLASS);
      attrObj.field(dao.USER_LOGIN_INFO, new ArrayList());
      attrObj.field(UserDao.GENERATED_USERNAME, generated);
      PermissionsHelper.grantRead(
          attrObj, RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString()));
      PermissionsHelper.changeOwner(attrObj, userRid);
      profile.field(dao.ATTRIBUTES_SYSTEM, attrObj);

      PermissionsHelper.grantRead(
          profile, RoleDao.getRole(DefaultRoles.REGISTERED_USER.toString()));
      PermissionsHelper.grantRead(profile, RoleDao.getRole(DefaultRoles.ANONYMOUS_USER.toString()));
      PermissionsHelper.changeOwner(profile, userRid);

      profile.field(dao.USER_SIGNUP_DATE, signupDate == null ? new Date() : signupDate);
      profile.save();

      DbHelper.commitTransaction();
    } catch (OSerializationException e) {
      DbHelper.rollbackTransaction();
      throw e;
    } catch (Exception e) {
      DbHelper.rollbackTransaction();
      throw e;
    }
    return profile;
  } // signUp
Beispiel #9
0
 public static String getUsernameByProfile(ODocument profile) throws InvalidModelException {
   UserDao dao = UserDao.getInstance();
   dao.checkModelDocument(profile);
   return (String) ((ODocument) profile.field("user")).field("name");
 }
Beispiel #10
0
 public static ODocument getUserProfilebyUsername(String username) throws SqlInjectionException {
   UserDao dao = UserDao.getInstance();
   ODocument userDetails = null;
   userDetails = dao.getByUserName(username);
   return userDetails;
 }
Beispiel #11
0
 public static List<ODocument> getUsers(QueryParams criteria) throws SqlInjectionException {
   UserDao dao = UserDao.getInstance();
   return dao.get(criteria);
 }