Guilherme Longo
Guilherme Longo

Reputation: 2308

dealing with static list

I have create a permission object that stores userId, Groups user is in and user´s permissions. This is a public class

I also need to have a static object that stores a list of those permissions objects that if a administration changes anything in the permissions all changes apply immediately for every logged user

I have a couple of questions:

  1. Should I create this static object when the first user logs in or there is a mechanism a should use to create that list before the first user log-in (For instance when we start our app on IIS)?
  2. Would it be easy to remove the item list for a specific user when it log-out?

This is a system requirement that permissions settings take effect as soon as the administrator make changes.

Edit 1:

public class permissionTemp
{
    public static Guid userGuid        { get; set; }
    public static string[] grupos      { get; set; }
    public static string[] permissoes  { get; set; }
}


public static class security
{
    public List<permissionTemp> userPermissionSet { get; set; }
}

Upvotes: 0

Views: 614

Answers (3)

Maarten
Maarten

Reputation: 22945

  1. You can use the Application_Start method in the global.asax to run some code when the website starts for the first time. This will run before the first request is processed.
  2. You can use the Session_End method in the global.asax to remove the item from the list. Also you can do it at the same time where you execute FormsAuthentication.SignOut (if you use Forms Authentication).

Note: I would use some locking mechanism to prevent multiple simultaneous access to the list. An alternative place to store the list would be in the WebCache. This is used by all users, so if it is updated by person x, next read from person y will be the updated version.

Upvotes: 1

Chris
Chris

Reputation: 1642

Think about a singleton, so you do not worry about creation time:

Singleton:

public class Permission
{
   private Permission()
   { }      

   private static Permission _instance = null;
   public static Permission Instance
   {
      get
      {
         if(_instance == null)
         {
            _instance = new Permission();
         }
         return _instance
      }
}

Now you can have access to the same instance with

Permission.Instance

The object is created at the first access. So in the private constructor you can add your code to read the permissions fom database.

Upvotes: 2

Sandeep
Sandeep

Reputation: 46

First of all i recommend to avoid creating static object for storing such sensetive information and also if any user has closed browser without clicking "Log out" then object will not be removed for that particular User.

Still if you need to do this to meet your requirement you can create it in that object in Applciation Start Event on Global.asax file when application start first time.

Upvotes: 0

Related Questions