0

I have close to 10 controllers that currently share the same code. The code is pretty simple, it just checks if a set of data is null and checks if the current user has permission to access the data.

If there is an issue, I throw an HttpResponseException.

The code works when it is sitting in each controller. I have also managed to centralize the code but I think the way I have done it is wrong. I've created a new class which inherits ApiController and then I have the controllers inheriting my new class. This is the only way I could get the HttpResponseExceptions working. Code is as follows:

//New centralized class:

public class AuthorizationClass : ApiController
{   
    private DataModel db = new DataModel();

    public async Task checkUserisValid(int user_id)
    {
        user_list user_list = await db.user_list.FindAsync(user_id);

        if (user_list == null)
        {
            throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest,"This user does not exist"));
        }

        int businessID = user_list.business_id;

        var result = checkAccess(User.Identity.Name, businessID);

        if (result.Count <= 0)
        {
            throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You do not have access to modify this business"));
        }
    }

    public static List<user_details> checkAccess(string userName, int id)
    {
        //code which checks if the user is in the right tables
            return checkAccess.ToList();
    }
}

Then in the controller class, I have:

    public class MyController : AuthorizationClass 
{
        public async Task<IHttpActionResult> Postnew_table(int id, new_table new_table)
        {
            await checkUserisValid(id);

        //rest of controller    
            }
}   

I tried to do it in different ways but this is the only way I could get it working with HttpResponseException. Is there a better way to do this without inheriting classes or is this the only way to do what I am after?

Thanks.

8
  • This isn't Classic ASP. It looks like it might be asp.net mvc Commented Jan 2, 2015 at 0:33
  • Apologies, I used the wrong tag! Commented Jan 2, 2015 at 0:36
  • 1
    why must this code be in the controller? Why not have a common assembly? But I also don't see anything wrong with it, every controller will inherit from AuthorizationClass? I guess that works. Commented Jan 2, 2015 at 0:39
  • 1
    why? maybe you should pass the dbcontext as an argument? make it work. Commented Jan 2, 2015 at 0:55
  • 1
    then pass it in as an argument. Commented Jan 2, 2015 at 1:43

1 Answer 1

1

You could just move these 2 methods to some static helper class in a common assembly, you mention that Request is an instance variable on the controller, just pass it to the method.

public static class SomeHelper
{
    public static async Task checkUserisValid(int user_id, DataModel db, Request request, User user)
    {
       user_list user_list = await db.user_list.FindAsync(user_id);

       if (user_list == null)
       {
          throw new   HttpResponseException(request.CreateErrorResponse(HttpStatusCode.BadRequest,"This user does not exist"));
        }

        int businessID = user_list.business_id;

        var result = checkAccess(user.Identity.Name, businessID);

        if (result.Count <= 0)
        {
          throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You do not have access to modify this business"));
        }
   }

  public static List<user_details> checkAccess(string userName, int id)
  {
      //code which checks if the user is in the right tables
          return checkAccess.ToList();
   }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.