UserManagement Class Code


This is code written in C# to give an idea of a framework for a UserManagement class. It relies on the functions in DatabaseCommon (the C# version) - which are also in DBOperations. It demonstrates a class that uses Static methods to return values from the database. It should give students a framework to make calls to the database. It also demonstrates encapsulating code into classes to organize your code. As part of assignments, students will add functions to this like validateUser, addUser, etc....

 

// Dependencies are DatabaseCommon class for accessing the database

   public class UserManagement
    {
        public UserManagement()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static String userReturnPage(System.Web.SessionState.HttpSessionState s)
        {
            if (UserManagement.isLoggedIn(s))
            {
               // This answers the page that the user should be routed to based on whether they are logged in or not

                return "~/main/default_loggedIn.aspx";
            }
            else
            {
                return "~/main/default.aspx";
            }
        }


        public static Boolean isLoggedIn(System.Web.SessionState.HttpSessionState s)
        {
          // When the user logs in , the UserID of the user should be stored in the session variable
          // under the key user_id

            return (Convert.ToInt32(s["user_id"]) > 0);
        }

public static DataSet getUser(int UserID)
        {
            DatabaseCommon db = new DatabaseCommon();
            Dictionary<string, object> Parameters = new Dictionary<string, object>();
            Parameters.Add("@UserID", UserID);
            return (DataSet)db.runStoredProcedure("sp_GetUser", Parameters);
        }
}