/// <summary> /// This Method will validate against the DB through a store proc to see /// if the user is indeed valid, if so then it will send the object to be /// solidify because by itself it won't be serializable due to its lazy dependencies /// Plus we want to send back only data that is important, not the username and password /// again since it can be hacked. /// Now that I think about it I would have to refactor as I did all in this function /// instead of delegating the work to the Document facade for the events and for the courses /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> public Dictionary <String, Object> login(String username, String password) { // Check that null or empty was not sent this far because it should've been handled it before if (username == null || username == "" || username == "undefined" && password == null || password == "" || password == "undefined") { return(null); } // Create instance for mapped functions to DBprocedures to retrive data WeLearnDBmsEntities dbEntities = new WeLearnDBmsEntities(); // Dictionary to be returned Dictionary <String, Object> loginData = new Dictionary <string, object>(); // Obtain results based on whether the person with username and password exists on the system // the var below is of return type -> ObjectResult<fun_IsUserInSystem_Result> var dbUserPropertiesLazyObjects = dbEntities.fun_IsUserInSystem(username, password); // Transform results in a list type -> List<fun_IsUserInSystem_Result> List <fun_IsUserInSystem_Result> dbUserProfileList = dbUserPropertiesLazyObjects.ToList(); // If list is empty then user is not valid since it did not validate againts the DB // If its valid send it over to get it solidified if (dbUserProfileList.Capacity > 0) { user = (UserSerializable)ReshapeProperties.solidifyDatabaseObjects(dbUserProfileList); } else { return(null); } // By now the user is validated and we have the user profile. Now retrive User Raw Courses Data // Return type -> ObjectResult<fun_GetOrRetriveAllCoursesFromUser_Result> var dbUserCoursesLazyObjs = dbEntities.fun_GetOrRetriveAllCoursesFromUser((long)user.getPersonId()); // Make the Complex object into a list List <fun_GetOrRetriveAllCoursesFromUser_Result> dbUserCoursesList = dbUserCoursesLazyObjs.ToList(); // Solidify User Courses if (dbUserCoursesList.Capacity > 0) { userCourses = (List <CoursesSerializable>)ReshapeProperties.solidifyDatabaseObjects(dbUserCoursesList); } // Get all the user Events -> "CE" Calendar Events // This var is of type -> ObjectResult<fun_GetOrRetriveAllCoursesFromUser_Result> var dbEventsLazyObjects = dbEntities.fun_GetOrRetriveUserDocuments((long)user.getPersonId(), "CE"); // Now Set them into a list List <fun_GetOrRetriveUserDocuments_Result> dbUserEventsList = dbEventsLazyObjects.ToList(); // Solidify them if (dbUserEventsList.Capacity > 0) { userEvents = (List <EventsSerializable>)ReshapeProperties.solidifyDatabaseObjects(dbUserEventsList); } // Lets wrap all into a dictionary loginData.Add("UserProfile", user); loginData.Add("UserCouses", userCourses); loginData.Add("UserEvents", userEvents);; //String testSerialization = JsonConvert.SerializeObject(loginData); // Lets send it back return(loginData); }
public void systemHandler(Dictionary <String, Object> data) { // Retrive context from dictionary HttpContext context = (HttpContext)data["HttpResponse"]; // Set type of expected string context.Response.ContentType = "text/html;charset=UTF-8"; // Initialize Values String username = ""; String password = ""; // Get parameters from url username = context.Request.Params["username"]; password = context.Request.Params["password"]; Dictionary <String, Object> portalData = null; // Send values to be evaluated try { // Double check just in the very remote case the front end did not handle well the input for these fields // before sending it remotely accross the web to see if it exists if (username == "undefined" || password == "undefined" || username == null || password == null || username == "" || password == "") { // So if the username field or password is missing when they first log in we will redirect and terminate // the execution of the code until the fields are completed properly // Since I am using the $locationProvider on the front end I don't need to specify the root since it will handle // all for me otherwise when it makes a 404 no found it will return the whole index html page context.Response.Redirect("", false); context.ApplicationInstance.CompleteRequest(); return; } // Communicate to remote function to get all info about person trying to log-in // Outside of classed is mapped to this -> private UserFacadeRemote personFacade; // In java the framework will handle the instantiation and removal of objects but // since I don't how to set it up in asp.net I had to do it this way // Instance of userFacade userFacade = new UserFacade(); // Execute Remote function call to evaluate the username and password portalData = userFacade.login(username, password); // Evaluate if there is a user with that usename and password if (portalData == null) { // Redirect with error, tipically its documented to use true // for the second parameter but that causes to through an exceptio // which is aborting thread which does the trick but its definetely // poor login so I use false which does the redirect but won't terminate the execution of the code // the following line will terminate the request and I use return to not allow any further code to be executed // Since I am using the $locationProvider on the front end I don't need to specify the root since it will handle // all for me otherwise when it makes a 404 no found it will return the whole index html page context.Response.Redirect("", false); context.ApplicationInstance.CompleteRequest(); return; } // Up to this point user should exist so lets Store User Locally // By retriving the user profile if (portalData.ContainsKey("UserProfile")) { userLogged = (UserSerializable)portalData["UserProfile"]; } else { // Redirect with error context.Response.Redirect("", false); context.ApplicationInstance.CompleteRequest(); return; } // and saving authentication token on session context.Session["userId"] = userLogged.getPersonId(); context.Session["userFistName"] = userLogged.getFirstName(); context.Session["userLastName"] = userLogged.getLastName(); // Return Serialized object to Http Request back in AngularJS //JavaScriptSerializer js = new JavaScriptSerializer(); //String testJSSerializer = js.Serialize(portalData); String testSerialization = JsonConvert.SerializeObject(portalData); // Send Object Serialized context.Response.Write(JsonConvert.SerializeObject(portalData)); } catch (Exception ex) { String debug = ex.ToString(); String de = debug; } }