Пример #1
0
        public static bool TryGettingBaseRightsOrDefault(out JArray _BaseRightsOrDefault, string _UserID, Action <string> _ErrorMessageAction)
        {
            if (!Controller_Rights_Internal.Get().GetUserDefaultRights(out _BaseRightsOrDefault, _UserID, _ErrorMessageAction))
            {
                return(false);
            }

            if (!GetUserBaseRights(_UserID, out JArray _ExistingBaseRights, _ErrorMessageAction))
            {
                return(false);
            }

            AccessScopeLibrary.UnionMergeRights(_BaseRightsOrDefault, _ExistingBaseRights);
            return(true);
        }
Пример #2
0
        private BWebServiceResponse AddUpdateBaseRightsForUser(HttpListenerContext _Context, Action <string> _ErrorMessageAction)
        {
            var NewBaseScopeListFromRequest = new List <AccessScope>();

            using (var InputStream = _Context.Request.InputStream)
            {
                using (var ResponseReader = new StreamReader(InputStream))
                {
                    try
                    {
                        var NewScopes = JArray.Parse(ResponseReader.ReadToEnd());
                        foreach (JObject NewScope in NewScopes)
                        {
                            var ConvertedScope = JsonConvert.DeserializeObject <AccessScope>(NewScope.ToString());
                            for (int i = ConvertedScope.AccessRights.Count - 1; i >= 0; i--)
                            {
                                ConvertedScope.AccessRights[i] = ConvertedScope.AccessRights[i].ToUpper();
                                if (!AccessScopeLibrary.ACCESS_RIGHTS.Contains(ConvertedScope.AccessRights[i]))
                                {
                                    ConvertedScope.AccessRights.RemoveAt(i);
                                }
                            }

                            if (ConvertedScope.AccessRights.Count > 0)
                            {
                                ConvertedScope.AccessRights = ConvertedScope.AccessRights.Distinct().ToList();
                                NewBaseScopeListFromRequest.Add(ConvertedScope);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        _ErrorMessageAction?.Invoke("User_AddListBaseRights_ForUser->AddUpdateBaseRightsForUser: Read request body stage has failed. Exception: " + e.Message + ", Trace: " + e.StackTrace);
                        return(BWebResponse.BadRequest("Malformed request body. Request must be a valid json form."));
                    }
                }
            }

            if (NewBaseScopeListFromRequest.Count == 0)
            {
                return(BWebResponse.BadRequest("Request does not contain any valid base scope. Access rights can be: " + AccessScopeLibrary.GetPossibleAccessRightsText()));
            }

            var UserKey = new BPrimitiveType(RequestedUserID);

            if (!DatabaseService.GetItem(
                    UserDBEntry.DBSERVICE_USERS_TABLE(),
                    UserDBEntry.KEY_NAME_USER_ID,
                    UserKey,
                    UserDBEntry.Properties,
                    out JObject UserObject,
                    _ErrorMessageAction))
            {
                return(BWebResponse.InternalError("Database fetch-user-info operation has failed."));
            }
            if (UserObject == null)
            {
                return(BWebResponse.NotFound("User does not exist."));
            }

            var NewBaseAccessScopeAsJArray = new JArray();
            var NewBaseAccessScopeAsList   = new List <AccessScope>();

            bool bUpdateOccurredForUserEntry = false;

            if (UserObject.ContainsKey(UserDBEntry.BASE_ACCESS_SCOPE_PROPERTY))
            {
                var BaseAccessScopeAsArray = (JArray)UserObject[UserDBEntry.BASE_ACCESS_SCOPE_PROPERTY];

                foreach (var NewScope in NewBaseScopeListFromRequest)
                {
                    bool bNewScopeFoundInExisting = false;

                    NewScope.AccessRights.Sort();

                    foreach (JObject ExistingScopeObject in BaseAccessScopeAsArray)
                    {
                        bool bChangeOccurredForScope = false;

                        var ExistingScope = JsonConvert.DeserializeObject <AccessScope>(ExistingScopeObject.ToString());
                        ExistingScope.AccessRights.Sort();

                        if (!bNewScopeFoundInExisting && ExistingScope.WildcardPath == NewScope.WildcardPath)
                        {
                            bNewScopeFoundInExisting = true;

                            if (NewScope.AccessRights.Count != ExistingScope.AccessRights.Count)
                            {
                                bUpdateOccurredForUserEntry = true;
                                NewBaseAccessScopeAsList.Add(NewScope);
                                NewBaseAccessScopeAsJArray.Add(JObject.Parse(JsonConvert.SerializeObject(NewScope)));
                                break;
                            }
                            else
                            {
                                bool bLocalChangeExists = false;

                                for (int i = 0; i < NewScope.AccessRights.Count; i++)
                                {
                                    if (NewScope.AccessRights[i] != ExistingScope.AccessRights[i])
                                    {
                                        bLocalChangeExists = true;
                                        break;
                                    }
                                }

                                if (bLocalChangeExists)
                                {
                                    bUpdateOccurredForUserEntry = true;
                                    bChangeOccurredForScope     = true;
                                    NewBaseAccessScopeAsList.Add(NewScope);
                                    NewBaseAccessScopeAsJArray.Add(JObject.Parse(JsonConvert.SerializeObject(NewScope)));
                                }
                            }
                        }

                        if (!bChangeOccurredForScope)
                        {
                            NewBaseAccessScopeAsList.Add(ExistingScope);
                            NewBaseAccessScopeAsJArray.Add(ExistingScopeObject);
                        }
                    }

                    if (!bNewScopeFoundInExisting)
                    {
                        NewBaseAccessScopeAsList.Add(NewScope);
                        NewBaseAccessScopeAsJArray.Add(JObject.Parse(JsonConvert.SerializeObject(NewScope)));
                        bUpdateOccurredForUserEntry = true;
                    }
                }
            }

            if (bUpdateOccurredForUserEntry)
            {
                UserObject[UserDBEntry.BASE_ACCESS_SCOPE_PROPERTY] = NewBaseAccessScopeAsJArray;

                MemoryService.SetKeyValue(CommonData.MemoryQueryParameters, new Tuple <string, BPrimitiveType>[]
                {
                    new Tuple <string, BPrimitiveType>(
                        UserBaseAccessMEntry.M_KEY_NAME_USER_ID + RequestedUserID,
                        new BPrimitiveType(JsonConvert.SerializeObject(new UserBaseAccessMEntry()
                    {
                        BaseAccessScope = NewBaseAccessScopeAsList
                    })))
                }, _ErrorMessageAction);

                Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget(
                    _Context,
                    UserDBEntry.DBSERVICE_USERS_TABLE(),
                    UserDBEntry.KEY_NAME_USER_ID,
                    UserKey,
                    UserObject);
            }

            return(BWebResponse.StatusCreated("New base rights have been added."));
        }
Пример #3
0
        private BWebServiceResponse UpdateBaseRightForUser(HttpListenerContext _Context, Action <string> _ErrorMessageAction)
        {
            var NewRights = new List <string>();

            using (var InputStream = _Context.Request.InputStream)
            {
                using (var ResponseReader = new StreamReader(InputStream))
                {
                    try
                    {
                        var NewRightsArray = JArray.Parse(ResponseReader.ReadToEnd());
                        foreach (string NewRight in NewRightsArray)
                        {
                            var NewRightCaseCorrected = NewRight.ToUpper();
                            if (AccessScopeLibrary.ACCESS_RIGHTS.Contains(NewRightCaseCorrected))
                            {
                                NewRights.Add(NewRightCaseCorrected);
                            }
                        }
                        NewRights = NewRights.Distinct().ToList();
                        NewRights.Sort();
                    }
                    catch (Exception e)
                    {
                        _ErrorMessageAction?.Invoke("User_UpdateDeleteBaseRight_ForUser->UpdateBaseRightForUser: Read request body stage has failed. Exception: " + e.Message + ", Trace: " + e.StackTrace);
                        return(BWebResponse.BadRequest("Malformed request body. Request must be a valid json form."));
                    }
                }
            }

            if (NewRights.Count == 0)
            {
                return(BWebResponse.BadRequest("Request does not contain any valid access right. Use DELETE method for deleting the scope. Access rights can be: " + AccessScopeLibrary.GetPossibleAccessRightsText()));
            }

            var UserKey = new BPrimitiveType(RequestedUserID);

            if (!DatabaseService.GetItem(
                    UserDBEntry.DBSERVICE_USERS_TABLE(),
                    UserDBEntry.KEY_NAME_USER_ID,
                    UserKey,
                    UserDBEntry.Properties,
                    out JObject UserObject,
                    _ErrorMessageAction))
            {
                return(BWebResponse.InternalError("Database fetch-user-info operation has failed."));
            }
            if (UserObject == null)
            {
                return(BWebResponse.NotFound("User does not exist."));
            }

            if (!UserObject.ContainsKey(UserDBEntry.BASE_ACCESS_SCOPE_PROPERTY))
            {
                return(BWebResponse.NotFound("User does not have any base rights."));
            }

            var BaseAccessScopeAsArray = (JArray)UserObject[UserDBEntry.BASE_ACCESS_SCOPE_PROPERTY];
            var BaseAccessScopeAsList  = new List <AccessScope>();

            //Check existence of access scope
            AccessScope ExistingAccessScope      = null;
            int         ExistingAccessScopeIndex = -1;

            int j = 0;

            foreach (JObject BaseAccessScopeObject in BaseAccessScopeAsArray)
            {
                var Scope = JsonConvert.DeserializeObject <AccessScope>(BaseAccessScopeObject.ToString());
                BaseAccessScopeAsList.Add(Scope);

                if (ExistingAccessScopeIndex == -1 && Scope.WildcardPath == RequestedBaseRightWildcard)
                {
                    ExistingAccessScope      = Scope;
                    ExistingAccessScopeIndex = j;
                }
                j++;
            }

            if (ExistingAccessScopeIndex == -1)
            {
                return(BWebResponse.NotFound("User does not have the given base right."));
            }

            ExistingAccessScope.AccessRights.Sort();

            //Check if requested rights are different
            bool bDifferent = false;

            if (ExistingAccessScope.AccessRights.Count == NewRights.Count)
            {
                for (var i = 0; i < ExistingAccessScope.AccessRights.Count; i++)
                {
                    if (ExistingAccessScope.AccessRights[i] != NewRights[i])
                    {
                        bDifferent = true;
                        break;
                    }
                }
            }
            else
            {
                bDifferent = true;
            }

            if (bDifferent)
            {
                ExistingAccessScope.AccessRights = NewRights;
                BaseAccessScopeAsArray[ExistingAccessScopeIndex] = JObject.Parse(JsonConvert.SerializeObject(ExistingAccessScope));

                UserObject[UserDBEntry.BASE_ACCESS_SCOPE_PROPERTY] = BaseAccessScopeAsArray;

                Controller_DeliveryEnsurer.Get().DB_UpdateItem_FireAndForget(
                    _Context,
                    UserDBEntry.DBSERVICE_USERS_TABLE(),
                    UserDBEntry.KEY_NAME_USER_ID,
                    UserKey,
                    UserObject);

                MemoryService.SetKeyValue(CommonData.MemoryQueryParameters, new Tuple <string, BPrimitiveType>[]
                {
                    new Tuple <string, BPrimitiveType>(
                        UserBaseAccessMEntry.M_KEY_NAME_USER_ID + RequestedUserID,
                        new BPrimitiveType(JsonConvert.SerializeObject(new UserBaseAccessMEntry()
                    {
                        BaseAccessScope = BaseAccessScopeAsList
                    })))
                }, _ErrorMessageAction);
            }

            return(BWebResponse.StatusOK("Base right has been updated."));
        }