Try our conversational search powered by Generative AI!

Need to track "Set Access Rights" activity

Vote:
 

Hi,

I want to track activity of the user for "Set Access Rights" section, Is there any way to create a custom logging for the same ?

Kindly let me know if anyone has any usefull info to track the above issue.

Thanks,

Chandrakant H

#199857
Dec 14, 2018 8:15
Vote:
 

Hi,

You could hook into the ContentSecuritySaved event in the IContentSecurityRepository.

Something like the following:

    [InitializableModule]
    [ModuleDependency(typeof(Web.InitializationModule))]
    public class SecuritySavedEventInitializationModule : IInitializableModule
    {
        private readonly ILogger _log = LogManager.GetLogger(typeof(SecuritySavedEventInitializationModule));

        public void Initialize(InitializationEngine context)
        {
            var contentSecurityRepository = ServiceLocator.Current.GetInstance<IContentSecurityRepository>();
            contentSecurityRepository.ContentSecuritySaved += LogContentSecuritySaved;
        }

        private void LogContentSecuritySaved(object sender, ContentSecurityEventArg e)
        {
            var principal = PrincipalInfo.CurrentPrincipal;
            var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

            IContent content;

            if (!contentLoader.TryGet(e.ContentLink, out content))
            {
                return;
            }

            IEnumerable<string> permissions = e.ContentSecurityDescriptor.Entries.Select(entry => $"{entry.Name}: {entry.Access.ToString()}");

            var message =
                $"{principal.Identity.Name} changed access rights for {content.Name} ({content.ContentLink}). Permissions: {string.Join(", ", permissions)}";

            _log.Log(Level.Information, message);

        }

        public void Uninitialize(InitializationEngine context)
        {
            var contentSecurityRepository = ServiceLocator.Current.GetInstance<IContentSecurityRepository>();
            contentSecurityRepository.ContentSecuritySaved -= LogContentSecuritySaved;
        }
    }
#199865
Dec 14, 2018 15:54
Vote:
 
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class EpiserverInitialization : IInitializableModule
{
    private ILogger _log;
    public void Initialize(InitializationEngine context)
    {
       _log = EPiServer.Logging.LogManager.GetLogger(typeof(EpiserverInitialization));
            var securityRepo = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentSecurityRepository>();
       securityRepo.ContentSecuritySaved += SecurityRepo_ContentSecuritySaved;
    }

    private void SecurityRepo_ContentSecuritySaved(object sender, ContentSecurityEventArg e)
    {
            _log.Information($"Content security has been changed for {e.ContentLink} by {HttpContext.Current.User.Identity.Name}");
    }
    public void Uninitialize(InitializationEngine context)
    {
        var securityRepo = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentSecurityRepository>();
        securityRepo.ContentSecuritySaved -= SecurityRepo_ContentSecuritySaved;
    }
}
#199866
Edited, Dec 14, 2018 16:03
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.