Ejemplo n.º 1
0
 internal static Task ClearCacheAsync(this Role role, CancellationToken cancellationToken, string correlationID = null, bool clearRelatedDataCache = true)
 => Task.WhenAll(new[]
 {
     clearRelatedDataCache?role.ClearRelatedCacheAsync(null, cancellationToken, correlationID) : Task.CompletedTask,
         Utility.Cache.RemoveAsync(role.Remove(), cancellationToken),
         Utility.RTUService.SendInterCommunicateMessageAsync(new CommunicateMessage(ServiceBase.ServiceComponent.ServiceName)
     {
         Type           = $"{role.GetObjectName()}#Delete",
         Data           = role.ToJson(),
         ExcludedNodeID = Utility.NodeID
     }, cancellationToken),
         Utility.WriteCacheLogs ? Utility.WriteLogAsync(correlationID, $"Clear cache of a role [{role.Title} - ID: {role.ID}]", ServiceBase.ServiceComponent.CancellationToken, "Caches") : Task.CompletedTask
 });
Ejemplo n.º 2
0
        static async Task <Tuple <List <UpdateMessage>, List <CommunicateMessage> > > DeleteChildrenAsync(this Role role, RequestInfo requestInfo, Func <RequestInfo, CancellationToken, Task> serviceCaller = null, Action <RequestInfo, string, Exception> onServiceCallerGotError = null, CancellationToken cancellationToken = default)
        {
            var updateMessages      = new List <UpdateMessage>();
            var communicateMessages = new List <CommunicateMessage>();
            var objectName          = role.GetTypeName(true);

            var children = await role.FindChildrenAsync(cancellationToken).ConfigureAwait(false);

            await children.ForEachAsync(async child =>
            {
                var messages        = await child.DeleteChildrenAsync(requestInfo, serviceCaller, onServiceCallerGotError, cancellationToken).ConfigureAwait(false);
                updateMessages      = updateMessages.Concat(messages.Item1).ToList();
                communicateMessages = communicateMessages.Concat(messages.Item2).ToList();
            }, true, false).ConfigureAwait(false);

            await Role.DeleteAsync <Role>(role.ID, requestInfo.Session.User.ID, cancellationToken).ConfigureAwait(false);

            await role.ClearCacheAsync(cancellationToken, requestInfo.CorrelationID, true).ConfigureAwait(false);

            // send notification
            role.SendNotificationAsync("Delete", role.Organization.Notifications, ApprovalStatus.Published, ApprovalStatus.Published, requestInfo, ServiceBase.ServiceComponent.CancellationToken).Run();

            // update users
            var beRemovedUserIDs = role.UserIDs ?? new List <string>();
            var parentRole       = role.ParentRole;

            while (parentRole != null)
            {
                beRemovedUserIDs = beRemovedUserIDs.Concat(parentRole.UserIDs ?? new List <string>()).ToList();
                parentRole       = parentRole.ParentRole;
            }
            beRemovedUserIDs = beRemovedUserIDs.Distinct(StringComparer.OrdinalIgnoreCase).ToList();
            var requestUser = new RequestInfo(requestInfo)
            {
                ServiceName = "Users",
                ObjectName  = "Privileges",
                Verb        = "POST",
                Query       = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    { "related-service", requestInfo.ServiceName },
                    { "related-object", "Role" },
                    { "related-system", role.SystemID },
                    { "related-entity", typeof(Role).GetTypeName() },
                    { "related-object-identity", role.ID }
                },
                Extra = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase)
                {
                    { "RemovedRoles", new[] { role.ID }.ToJArray().ToString(Formatting.None).Encrypt(Utility.EncryptionKey) }
                }
            };
            await beRemovedUserIDs.ForEachAsync(async userID =>
            {
                try
                {
                    requestUser.Query["object-identity"] = userID;
                    await(serviceCaller == null ? Task.CompletedTask : serviceCaller(requestUser, cancellationToken)).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    onServiceCallerGotError?.Invoke(requestUser, $"Error occurred while updating roles of an user account [{userID}] => {ex.Message}", ex);
                }
            }, true, false).ConfigureAwait(false);

            var json = role.ToJson();

            updateMessages.Add(new UpdateMessage
            {
                Type     = $"{requestInfo.ServiceName}#{objectName}#Delete",
                Data     = json,
                DeviceID = "*"
            });
            communicateMessages.Add(new CommunicateMessage(requestInfo.ServiceName)
            {
                Type           = $"{objectName}#Delete",
                Data           = json,
                ExcludedNodeID = Utility.NodeID
            });
            return(new Tuple <List <UpdateMessage>, List <CommunicateMessage> >(updateMessages, communicateMessages));
        }