vicinityManager/server/services/sharingRules.js
/*
A user changes its access level
I need check that all devices have an AL below users AL
If not, I need to update all items and contracts accordingly
*/
A user changes its access level
I need check that all devices have an AL below users AL
If not, I need to update all items and contracts accordingly
A user changes its access level I need check that all devices have an AL below users AL If not I need to update all items and contracts accordingly
function changeUserAccessLevel(uid, newAccessLevel, email){
  var items = [], updItems = [];
  return new Promise(function(resolve, reject) {
    itemOp.find({'uid.id':uid, accessLevel: {$gt: newAccessLevel}}, {hasContracts:1, oid:1, cid:1, accessLevel:1, typeOfItem:1}).populate('cid.id', 'knows')
    .then(function(items){
      var aux = {}, ids = [];
      for(var i = 0; i < items.length; i++){
        aux = items[i].toObject();
        ids.push(aux._id);
        aux.accessLevel = newAccessLevel;
        updItems.push(aux);
      }
      return itemOp.update({_id: {$in: ids}}, {$set: {accessLevel: newAccessLevel}},{multi:true});
    })
    .then(function(response){
      if(updItems.length !== 0){ 
        logger.debug('Start async handler...');
        sync.forEachAll(updItems,
          function(value, allresult, next, otherParams) {
            ctHelper.removeDevice(value, otherParams, function(value, result) {
                allresult.push({value: value, error: result});
                next();
            });
          },
          function(allresult) {
            if(allresult.length === updItems.length){
              logger.debug('Completed async handler: ' + JSON.stringify(allresult));
              resolve({"error": false, "message": allresult });
            }
          },
          false,
          {email: email}
        );
      } else {
        logger.debug('Change user accessLevel: No items to reduce accessLevel');
        logger.warn({user:email, action: 'removeContract', message: "Nothing to be removed"});
        resolve({"error": false, "message": "Nothing to be removed..."});
      }
    })
    .catch(function(error){
      logger.debug('Error remove friend: ' + error);
      reject({error: true, message: error});
    });
  });
}
function changeUserAccessLevel ( uid , newAccessLevel , email ) { var items = [ ] , updItems = [ ] ; return new Promise ( function ( resolve , reject ) { itemOp . find ( { 'uid.id' : uid , accessLevel : { $gt : newAccessLevel } } , { hasContracts : 1 , oid : 1 , cid : 1 , accessLevel : 1 , typeOfItem : 1 } ) . populate ( 'cid.id' , 'knows' ) . then ( function ( items ) { var aux = { } , ids = [ ] ; for ( var i = 0 ; i < items . length ; i ++ ) { aux = items [ i ] . toObject ( ) ; ids . push ( aux . _id ) ; aux . accessLevel = newAccessLevel ; updItems . push ( aux ) ; } return itemOp . update ( { _id : { $in : ids } } , { $set : { accessLevel : newAccessLevel } } , { multi : true } ) ; } ) . then ( function ( response ) { if ( updItems . length !== 0 ) { logger . debug ( 'Start async handler...' ) ; sync . forEachAll ( updItems , function ( value , allresult , next , otherParams ) { ctHelper . removeDevice ( value , otherParams , function ( value , result ) { allresult . push ( { value : value , error : result } ) ; next ( ) ; } ) ; } , function ( allresult ) { if ( allresult . length === updItems . length ) { logger . debug ( 'Completed async handler: ' + JSON . stringify ( allresult ) ) ; resolve ( { "error" : false , "message" : allresult } ) ; } } , false , { email : email } ) ; } else { logger . debug ( 'Change user accessLevel: No items to reduce accessLevel' ) ; logger . warn ( { user : email , action : 'removeContract' , message : "Nothing to be removed" } ) ; resolve ( { "error" : false , "message" : "Nothing to be removed..." } ) ; } } ) . catch ( function ( error ) { logger . debug ( 'Error remove friend: ' + error ) ; reject ( { error : true , message : error } ) ; } ) ; } ) ; }
A user changes its access level
I need check that all devices have an AL below users AL
If not, I need to update all items and contracts accordingly
A user changes its access level I need check that all devices have an AL below users AL If not I need to update all items and contracts accordingly
// Check if there is any item to modify // Removes invalid contracts from all affected devices