Esempio n. 1
0
 private bool UpdateUsersSharedModelsFields(List <string> _RelevantIdList, string _ModelID, Controller_Rights_Internal.EChangeUserRightsForModelType _Action, Action <string> _ErrorMessageAction)
 {
     foreach (var ChangeUserID in _RelevantIdList)
     {
         if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UserDBEntry.DBSERVICE_USERS_TABLE(), ChangeUserID, _ErrorMessageAction))
         {
             _ErrorMessageAction?.Invoke("PubSub_To_AuthService->UpdateUsersSharedModelsFields: Atomic operation control has failed for " + UserDBEntry.KEY_NAME_USER_ID + ": " + ChangeUserID);
             return(false); //Retry
         }
         try
         {
             if (!DatabaseService.GetItem(
                     UserDBEntry.DBSERVICE_USERS_TABLE(),
                     UserDBEntry.KEY_NAME_USER_ID,
                     new BPrimitiveType(ChangeUserID),
                     UserDBEntry.Properties,
                     out JObject UserObject,
                     _ErrorMessageAction))
             {
                 _ErrorMessageAction?.Invoke("PubSub_To_AuthService->UpdateUsersSharedModelsFields: Get user database entry operation has failed. User ID: " + ChangeUserID);
                 return(false); //Retry
             }
             else if (UserObject != null)
             {
                 bool bUpdateDB        = false;
                 var  UserDeserialized = JsonConvert.DeserializeObject <UserDBEntry>(UserObject.ToString());
                 if (_Action == Controller_Rights_Internal.EChangeUserRightsForModelType.Add)
                 {
                     if (!UserDeserialized.UserSharedModels.Contains(_ModelID))
                     {
                         UserDeserialized.UserSharedModels.Add(_ModelID);
                         bUpdateDB = true;
                     }
                 }
                 else if (_Action == Controller_Rights_Internal.EChangeUserRightsForModelType.Delete)
                 {
                     if (UserDeserialized.UserSharedModels.Contains(_ModelID))
                     {
                         UserDeserialized.UserSharedModels.Remove(_ModelID);
                         bUpdateDB = true;
                     }
                 }
                 if (bUpdateDB)
                 {
                     if (!DatabaseService.UpdateItem(//Fire and forget is not suitable here since there are following calls after DB update which will change the DB structure
                             UserDBEntry.DBSERVICE_USERS_TABLE(),
                             UserDBEntry.KEY_NAME_USER_ID,
                             new BPrimitiveType(ChangeUserID),
                             JObject.Parse(JsonConvert.SerializeObject(UserDeserialized)),
                             out JObject _, EBReturnItemBehaviour.DoNotReturn, null, _ErrorMessageAction))
                     {
                         _ErrorMessageAction?.Invoke("PubSub_To_AuthService->UpdateUsersSharedModelsFields: Update user database entry operation has failed. User ID: " + ChangeUserID);
                         return(false); //Retry
                     }
                 }
             }
         }
Esempio n. 2
0
            private bool UpdateRightsForUsersUponChangeOnSharing(List <string> _RelevantIdList, Tuple <string, List <string> >[] _RightsRegex, Controller_Rights_Internal.EChangeUserRightsForModelType _Action, Action <string> _ErrorMessageAction = null)
            {
                var WaitFor       = new ManualResetEvent(false);
                var InternalError = new BValue <bool>(false, EBProducerStatus.MultipleProducer);
                var DoneStack     = new ConcurrentStack <bool>();

                for (var i = 0; i < _RelevantIdList.Count; i++)
                {
                    for (var j = 0; j < _RightsRegex.Length; j++)
                    {
                        DoneStack.Push(true);
                    }
                }

                foreach (var ChangeForUserId in _RelevantIdList)
                {
                    var CurrentUserID = ChangeForUserId;
                    foreach (var PathRegex in _RightsRegex)
                    {
                        var CurrentPathRegex = PathRegex;
                        BTaskWrapper.Run(() =>
                        {
                            if (!Controller_AtomicDBOperation.Get().GetClearanceForDBOperation(InnerProcessor, UserDBEntry.DBSERVICE_USERS_TABLE(), CurrentUserID, _ErrorMessageAction))
                            {
                                _ErrorMessageAction?.Invoke("PubSub_To_AuthService->UpdateRightsForUsersUponChangeOnSharing: Atomic operation control has failed for " + UserDBEntry.KEY_NAME_USER_ID + ": " + CurrentUserID);
                                InternalError.Set(true);
                                try { WaitFor.Set(); } catch (Exception) { }
                                return;
                            }
                            try
                            {
                                if (!Controller_Rights_Internal.Get().ChangeBaseUserRight(
                                        out bool _bInternalErrorOccured,
                                        true,/*important to set to true*/
                                        _Action,
                                        CurrentUserID,
                                        CurrentPathRegex.Item1.Replace("{shareeUserId}", ChangeForUserId, StringComparison.InvariantCulture),
                                        _ErrorMessageAction,
                                        CurrentPathRegex.Item2))
                                {
                                    if (_bInternalErrorOccured)
                                    {
                                        InternalError.Set(true);
                                        try { WaitFor.Set(); } catch (Exception) { }
                                        return;
                                    }
                                }
                            }
                            finally
                            {
                                Controller_AtomicDBOperation.Get().SetClearanceForDBOperationForOthers(InnerProcessor, UserDBEntry.DBSERVICE_USERS_TABLE(), CurrentUserID, _ErrorMessageAction);
                            }

                            DoneStack.TryPop(out bool _);
                            if (DoneStack.Count == 0)
                            {
                                try
                                {
                                    WaitFor.Set();
                                }
                                catch (Exception) { }
                            }
                        });
                    }
                }

                try
                {
                    if (_RelevantIdList.Count > 0)
                    {
                        WaitFor.WaitOne();
                    }
                    WaitFor.Close();
                }
                catch (Exception) { }

                //Retry if internal error occured
                return(InternalError.Get() == false);
            }