Example #1
0
        public static IAccessAuthorization AccessAuthorize(this ISecurityStore store, ISecurityIdentity securityIdentity, ISecurityItem securityItem, string id = null)
        {
            if (string.IsNullOrEmpty(id))
            {
                id = Guid.NewGuid().ToString();
            }
            SecurityStore       securityStore       = (SecurityStore)store;
            AccessAuthorization accessAuthorization = new AccessAuthorization((SecurityStore)store, id)
            {
                SecurityIdentity = securityIdentity, SecurityItem = securityItem
            };

            securityStore.Authorizations.Add(accessAuthorization);
            return(accessAuthorization);
        }
        private Dictionary <string, IAuthorization> SyncAccessAuthorizations(IEnumerable <IStorageAccessAuthorization> storageAccessAuthorizations, Dictionary <string, ISecurityItem> securityItems,
                                                                             Dictionary <string, ISecurityIdentity> securityIdentities)
        {
            Dictionary <string, IStorageAccessAuthorization> accessAuthorizations =
                storageAccessAuthorizations.ToDictionary(a => a.Id);
            Dictionary <string, IAuthorization> authorizations = _store.Authorizations.ToDictionary(a => a.Id);

            foreach (string accessAuthorizationId in accessAuthorizations.Keys)
            {
                if (!authorizations.ContainsKey(accessAuthorizationId))
                {
                    IStorageAccessAuthorization storageAccessAuthorization = accessAuthorizations[accessAuthorizationId];
                    if (!securityItems.TryGetValue(storageAccessAuthorization.SecurityItemId, out ISecurityItem securityItem))
                    {
                        continue;
                    }
                    if (!securityIdentities.TryGetValue(storageAccessAuthorization.SecurityIdentityId,
                                                        out ISecurityIdentity securityIdentity))
                    {
                        continue;
                    }

                    IAccessAuthorization accessAuthorization =
                        _store.AccessAuthorize(securityIdentity, securityItem, accessAuthorizationId);
                    accessAuthorization.LifeTime =
                        (IAuthorizationLifeTime)DeserializeObject(storageAccessAuthorization.LifeTime);
                    authorizations.Add(accessAuthorizationId, accessAuthorization);
                }
            }

            foreach (string accessAuthorizationId in authorizations.Keys)
            {
                if (!accessAuthorizations.ContainsKey(accessAuthorizationId))
                {
                    AccessAuthorization accessAuthorization = (AccessAuthorization)authorizations[accessAuthorizationId];
                    AddNewAction(StorageActionType.Add,
                                 new StorageAccessAuthorization(accessAuthorizationId, accessAuthorization.SecurityIdentity.Id,
                                                                accessAuthorization.SecurityItem.Id, ToByteArray(accessAuthorization.LifeTime),
                                                                accessAuthorization.AccessType));
                }
            }

            return(authorizations);
        }