logo

Creating module-level events

When you create additional ELMA modules, you can define the actions that will be executed, when a module is installed/enabled/disabled/… in the system.

This article shows how to create a new user group when a new module is installed on the system. To do this, you need to implement IModuleEvents and IModuleContainerEvents extension points.

Attention
You must implement this extension point in the server part (back end) of your module.

IModuleEvents interface has the following methods:

/// <summary>
/// Installing the module
/// </summary>
/// <param name="module">Web part of the module</param>
void Installing(IWebModuleUnit module);
 
/// <summary>
/// Module is installed
/// </summary>
/// <param name="module">Web part of the module</param>
void Installed(IWebModuleUnit module);
 
/// <summary>
/// Enabling the module
/// </summary>
/// <param name="module">Web part of the module</param>
void Enabling(IWebModuleUnit module);
 
/// <summary>
/// Module is enabled
/// </summary>
/// <param name="module">Web part of the module</param>
void Enabled(IWebModuleUnit module);
 
/// <summary>
/// Disabling the module
/// </summary>
/// <param name="module">Web part of the module</param>                
void Disabling(IWebModuleUnit module);
 
/// <summary>
/// Module is disabled
/// </summary>
/// <param name="module">Web part of the module</param>        
void Disabled(IWebModuleUnit module);
 
 
/// <summary>
/// Uninstalling the module
/// </summary>
/// <param name="module">Web part of the module</param>        
void Uninstalling(IWebModuleUnit module);
 
/// <summary>
/// Module is uninstalled
/// </summary>
/// <param name="module">Web part of the module</param>        
void Uninstalled(IWebModuleUnit module);

IModuleContainerEvents interface has the following methods:

/// <summary>
/// Container is activated (all modules and extension points are loaded) 
/// This method is called after the container is activated 
/// When ELMA does activate the container:
/// 1) When ELMA server is started
/// 2) When ELMA recreates the container (this happens when a module is enabled/disabled)
/// </summary>
void Activated();
         
/// <summary>
/// Destroying the container
/// </summary>
void Terminating();

In this case, the implementation of these extension points is:

        [Component]
        public class MyModuleEvents : IModuleEvents, IModuleContainerEvents
        {
            private readonly Guid _guid = new Guid("7283F18A-D4DD-492D-B6B8-BC955A38115B");

            public IUserGroup Group ()
            {
                var group = UserGroupManager.Instance.Find(a => a.Uid == _guid).FirstOrDefault();
                return @group;
            }

            public void CreateGroups ()
            {
                if (Group() == null)
                {
                    var group = UserGroupManager.Instance.Create();
                    group.CreationAuthor = UserManager.Instance.Load(SecurityConstants.SystemUserUid);
                    group.CreationDate = DateTime.Now;
                    group.Name = "Group, created when a module was activated";
                    group.Description = "Group, created when a module was activated";
                    group.Groups.Add(UserGroupManager.Instance.Load(SecurityConstants.AdminGroupUid));
                    group.IsSystem = true;
                    group.Uid = _guid;
                    group.Save();
                }
            }

            public void Activated ()
            {
                CreateGroups();

                var securityService = Locator.GetService<SecurityService>();
                if (securityService != null)
                {
                    securityService.ResetPermissionsCache();
                }
            }

            public void Terminating ()
            {
            }

            public void Installing (IWebModuleUnit module)
            {
            }

            public void Installed (IWebModuleUnit module)
            {
                CreateGroups();
            }

            public void Enabling (IWebModuleUnit module)
            {
            }

            public void Enabled (IWebModuleUnit module)
            {
            }

            public void Disabling (IWebModuleUnit module)
            {
            }

            public void Disabled (IWebModuleUnit module)
            {
            }

            public void Uninstalling (IWebModuleUnit module)
            {
            }

            public void Uninstalled (IWebModuleUnit module)
            {
            }
        }

As a result, you will see the following:

module events

You can learn more about these extension points in ELMA API help: IModuleEvents and IModuleContainerEvents

Attachments