/// <summary> /// Performs the login into the application. /// </summary> /// <param name="login">Login of the user</param> /// <param name="password">Password of the user</param> /// <returns>An User DTO representing the logged user and session if the login /// is ok, null otherwise</returns> public static RecursoDTO Login(string login, string password) { using (EntitiesModel context = new EntitiesModel()) { // Retrieve user from database with the given login/pass combination WEB_RECURSO recurso = context.WEB_RECURSOs.Where(p => p.EMAIL == login && p.PASS_RECURSO == password).FirstOrDefault(); // If the combination user/pass is not correct, then return null information. if (recurso == null) { return null; } // Create a new session token GUID without scores. string guid = Guid.NewGuid().ToString().Replace("-", ""); // Build the user information DTO from the user entity and the session token RecursoDTO result = new RecursoDTO(); result.Fill(recurso); result.SessionToken = guid; // Remove other sessions of this user WEB_SESSION session = context.WEB_SESSIONs.Where(p => p.User_id == recurso.ID_RECURSO).FirstOrDefault(); if (session != null) { context.Delete(session); } // Build the session entity session = new WEB_SESSION(); session.Session_guid = result.SessionToken; session.User_id = result.Id_Recurso; session.Creation_dttm = DateTime.UtcNow; // Insert the session into database context.Add(session); // Commit changes context.SaveChanges(); return result; } }
/// <summary> /// Gets the user information from a session token. /// It's useful when a user has logged in and the session token is stored /// in the local storage, so entering the page can send the session token and /// enter the application without login in again. /// </summary> /// <param name="sessionToken">Session token GUID</param> /// <returns>User information attached to this session token, or null if the /// session token is invalid or expired.</returns> public static RecursoDTO GetUserInfo(string sessionToken) { using (EntitiesModel context = new EntitiesModel()) { WEB_SESSION session = context.WEB_SESSIONs.Where(p => p.Session_guid == sessionToken).FirstOrDefault(); if (session == null) { return null; } RecursoDTO result = new RecursoDTO(); result.Fill(session.WEB_RECURSO); result.SessionToken = sessionToken; return result; } }
/// <summary> /// Performs the logout from a session token. /// </summary> /// <param name="sessionToken">Session token GUID</param> public static void Logout(string sessionToken) { using (EntitiesModel context = new EntitiesModel()) { WEB_SESSION session = context.WEB_SESSIONs.Where(p => p.Session_guid == sessionToken).FirstOrDefault(); if (session != null) { context.Delete(session); context.SaveChanges(); } } }
/// <summary> /// Gets a context manager from a session token, representing a context filtered /// by the information of the user. /// </summary> /// <param name="sessionToken">Session token GUID</param> /// <returns>Context Manager instance for this session, or null if the /// session is invalid or expired.</returns> public static ContextManager GetContext(string sessionToken) { using (EntitiesModel context = new EntitiesModel()) { WEB_SESSION session = context.WEB_SESSIONs.Where(p => p.Session_guid == sessionToken).FirstOrDefault(); if (session == null) { return null; } RecursoDTO recurso = new RecursoDTO(); recurso.Fill(session.WEB_RECURSO); recurso.SessionToken = sessionToken; return new ContextManager(recurso); } }