A question came up recently that makes for a great example plugin. The question was, how can we clean up old user accounts that are no longer active on the community? Before we dive into the plugin there are a couple of things to consider when deleting user accounts, be it automatically or manually.

Creating the plugin

If you don’t know what a plugin is, and you haven’t read the post  Creating a Plugin for Telligent Community, please take a look to familiarize yourself with writing plugins first.

Creating a plugin that runs on a recurring interval is very easy, and requires your plugin to implement IRecurringEvolutionJobPlugin. Along with the IPlugin properties and method that must be implemented, it requires you to implement 3 simple properties and the method that will be executed each time the task is triggered by the service. Let’s take a look at the code.

Firstly, we need to provide a Guid that will be the unique identifier. You can use the tool of your preference to create.

        public Guid JobTypeId
            get { return Guid.Parse("92451a8c3a444e89ad8253efbc9220b6"); }

Next, we need to set the schedule that the job will run. In this example I am setting it to run every day at 2am. The JobSchedulder class has lots of static methods to quickly create most scheduler, such as every x minute, every x hour etc.

        public JobSchedule DefaultSchedule
            get { return JobSchedule.Daily(DateTime.Now.Date.AddHours(2.0)); }

The final property sets the context the job runs in. Some jobs need to run in the web process but this one is best run in the Windows service.

        public JobContext SupportedContext
            get { return JobContext.Service; }

The Logic

The last thing we need to do is write the code to loop through our users, and based on the logic, delete them if necessary. The Execute method is what is called each time the job is triggered and is where we will add our logic.
In this example, we will delete users that haven’t logged in for more than a year and don’t have any posts.

Note! The LastLoginDate property for a user is not updated when using SSO modules such as Cookie Authentication. If you are using SSO, you should consider different logic for your rule.
        public void Execute(JobData jobData)
            var moreRecords = true;
            var pageIndex = 0;
            var pageSize = 100;

                var users = PublicApi.Users.List(new UsersListOptions() {PageSize = pageSize, PageIndex = pageIndex});
                moreRecords = users.Count == pageSize;

                foreach (var user in users)
                    if (user.LastLoginDate < DateTime.Now.AddYears(-5) && user.TotalPosts == 0)
                        PublicApi.Users.Delete(new UsersDeleteOptions
                            Id = user.Id,
                            ReassignedUsername = PublicApi.Users.AnonymousUserName

            } while (moreRecords);

And finally….

In a subsequent post I will explore extending this project, and adding some more real work scenarios, like adding configuration and logging. I’m happy to receive your feedback and comments below, and if you have any more ideas for example projects, please do let me know.

You can see and fork the code for this example on GitHub.

Warning! This code has not been tested and comes with no warranty or guarantee. Extra care should be taken when using this code, especially as it involves deleting data. Please ensure you test it before deploying to a live environment, and always take backups.

Leave a Reply