Beispiel #1
0
        public static IConditionalAuthorization ConditionalAuthorize(this ISecurityStore store, ISecurityIdentity securityIdentity, ISecurityItem securityItem, string id = null)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = Guid.NewGuid().ToString();
            }

            SecurityStore            securityStore            = (SecurityStore)store;
            ConditionalAuthorization conditionalAuthorization = new ConditionalAuthorization((SecurityStore)store, id)
            {
                SecurityIdentity = securityIdentity, SecurityItem = securityItem
            };

            securityStore.Authorizations.Add(conditionalAuthorization);
            return(conditionalAuthorization);
        }
        private Dictionary <string, IAuthorization> SyncConditionAuthorizations(IEnumerable <IStorageConditionAuthorization> storageConditionAuthorizations, Dictionary <string, ISecurityItem> securityItems,
                                                                                Dictionary <string, ISecurityIdentity> securityIdentities)
        {
            Dictionary <string, IStorageConditionAuthorization> conditionAuthorizations =
                storageConditionAuthorizations.ToDictionary(a => a.Id);
            Dictionary <string, IAuthorization> authorizations = _store.Authorizations.ToDictionary(a => a.Id);

            foreach (string conditionAuthorizationId in conditionAuthorizations.Keys)
            {
                if (!authorizations.ContainsKey(conditionAuthorizationId))
                {
                    IStorageConditionAuthorization conditionAuthorization = conditionAuthorizations[conditionAuthorizationId];
                    if (!securityItems.TryGetValue(conditionAuthorization.SecurityItemId, out ISecurityItem securityItem))
                    {
                        continue;
                    }
                    if (!securityIdentities.TryGetValue(conditionAuthorization.SecurityIdentityId,
                                                        out ISecurityIdentity securityIdentity))
                    {
                        continue;
                    }

                    IConditionalAuthorization conditionalAuthorization =
                        _store.ConditionalAuthorize(securityIdentity, securityItem, conditionAuthorizationId);
                    conditionalAuthorization.LifeTime =
                        (IAuthorizationLifeTime)DeserializeObject(conditionAuthorization.LifeTime);
                    authorizations.Add(conditionAuthorizationId, conditionalAuthorization);
                }
            }

            foreach (string conditionAuthorizationId in authorizations.Keys)
            {
                if (!conditionAuthorizations.ContainsKey(conditionAuthorizationId))
                {
                    ConditionalAuthorization conditionalAuthorization = (ConditionalAuthorization)authorizations[conditionAuthorizationId];
                    AddNewAction(StorageActionType.Add,
                                 new StorageConditionAuthorization(conditionAuthorizationId, conditionalAuthorization.SecurityIdentity.Id,
                                                                   conditionalAuthorization.SecurityItem.Id, ToByteArray(conditionalAuthorization.LifeTime),
                                                                   ToByteArray(conditionalAuthorization.Conditions)));
                }
            }

            return(authorizations);
        }