Tuesday, 16 April 2013

Using CSharpRepl to batch change TeamMentor’s users email and settings

While deploying the 3.3. version of TeamMentor into the multiple servers we maintain, there are a number of edge cases that are usually a pain to deal with, but now that TM has the ability to run C# scripts directly on a server, I can codify the transformations required :)

The two problems I’m going to deal here are:
  • Batch setting the email based on the username
  • Batch setting the Account expiry date (based on a search)
Note that on both cases I’m going to first do the changes into a locally hosted version of TM that is consuming the respective UserData GitHub repo (and if anything goes wrong, I’m a git stash or git reset away from a clean start)

Batch setting the email based on the username

This problem happened because in one of the 3.2 TM sites that we hosted,  61 user accounts were created with no email value (in the previous version the email was not required). Luckily they used the email as the username, so this is ‘just’ a case of assigning an user’s email value to its username.

Let’s start by opening up a CSharpRepl script environment:

image

And this is how to get a reference to the current user’s object:

image

There are also extension methods that allow the easy programmatic access to a particular user:

image

And its email:

image

As a first example, let’s change this user’s email (which is dinis@si)

image

to dcruz@securityinnovation.com

image

Note that although this change shows in the User’s list

image

A git status will show that there was no file changes  (i.e. the change was all done in memory)

image

but if we save the user

image

the git status will now show a changed file:

image

(note that when running TM on localhost, the  auto git commits are disabled (namely to prevent conflicts with the admin account))

This means that the script to set all users email to its username is simply:

image

(the “admin” and “dinis” accounts were the only ones that the username didn't contain an email)

After running the script there were 61 changes, committed as one:

image

and pushed into the main Site_clientName repository.

After the push, I went into the live server and did a User Data Sync in order to do trigger the pull:

image

And now the user’s changes have been propagated to the live server:

image


Let’s now handle the 2nd issue:

Batch setting the Account expiry date (based on a search)

After the import from 3.2 to 3.3, we have 427 users in the current TM instance

image

In the CSharpRepl we start again by getting the TMUsers object (part of the UserData class)

image

Here is how to get the current users that have an SI account:

image

or if you prefer to use LINQ

image

Here is how to get a user’s account ExpirationDate:

image

which at the moment is (5th April 2013):

image

Here is how to make the expiry date to be in 16 days from today’s date:

image

And to disable completely the Expiration date, we just set it to default(DateTime);
image

And here is how to remove the expiration date check from all SI users:

image

Using TM4TM to document these scripts

Based on the scripts created on this blog post, I created the User Management using REPL Scripts page, written in WikiText

image

and whose HTML (and code formatting) looks like this:

image
Post a Comment