protected void Page_Load(object sender, EventArgs e) { ActivityTracker.Track("Dashboard", (int)UserActionEnum.Navigated); if (Page.User.Identity.IsAuthenticated && Session["UserId"] != null) { if (!Page.User.IsInRole("Administrator")) { pnlUserManager.Visible = false; pnlSlideShowManager.Visible = false; pnlFeedbackManager.Visible = false; pnlActivityTracker.Visible = false; pnlFitBitMonitor.Visible = false; pnlProgramsManager.Visible = false; } // Grab current user Id string userId = Session["UserId"].ToString(); // Query the db and grab the data using the Id HPSUser user = db.HPSUsers.Where(u => u.UserId == userId).SingleOrDefault(); // Display first name in the dashboard h1 tag welcome.InnerText = "Welcome " + user.FirstName + " " + user.LastName + "!"; } else { Response.Redirect("/Main.aspx"); } }
protected void btnCreateUser_Click(object sender, EventArgs e) { ActivityTracker.Track("Created a New User", (int)UserActionEnum.Created); try { // Create instance of role manager and store RoleStore <IdentityRole> roleStore = new RoleStore <IdentityRole>(); RoleManager <IdentityRole> roleMgr = new RoleManager <IdentityRole>(roleStore); if (!roleMgr.RoleExists(ddlRole.Text)) { IdentityResult roleResult = roleMgr.Create(new IdentityRole(ddlRole.Text)); } // Declare UserStore and UserManager UserStore <IdentityUser> userStore = new UserStore <IdentityUser>(); UserManager <IdentityUser> manager = new UserManager <IdentityUser>(userStore); // Declare/create new user and store in manager object in the userstore IdentityUser user = new IdentityUser(txtUsername.Text); user.Email = txtEmail.Text; // Store result of user creation IdentityResult idResult = manager.Create(user, txtPassword.Text); // Check if user was created and added to role if (idResult.Succeeded) { // Add user to role IdentityResult userResult = manager.AddToRole(user.Id, ddlRole.SelectedValue); lblMessage.Text = "User " + user.UserName + " was created successfully!"; lblMessage.CssClass = "text-success"; // Add other user information to separate table HPSUser hpsUser = new HPSUser(); hpsUser.FirstName = txtUserFirstName.Text; hpsUser.LastName = txtUserLastName.Text; hpsUser.CreatedOn = DateTime.Now; hpsUser.UserId = user.Id; hpsUser.RoleName = ddlRole.SelectedValue; db.HPSUsers.Add(hpsUser); // Add empty row to db for fitbit to help with update if sign up date is on the same day as a synchronization db.Steps.AddOrUpdate(new Step { StepCount = 0, StepDate = DateTime.Now.AddDays(-28), UserId = user.Id }); db.Distances.AddOrUpdate(new Distance { DistanceCount = 0, DistanceDate = DateTime.Now.AddDays(-28), UserId = user.Id }); db.Minutes.AddOrUpdate(new Minute { MinuteCount = 0, MinuteDate = DateTime.Now.AddDays(-28), UserId = user.Id }); // create notification for admin users string[] role = { "Administrator" }; NotificationCreator.CreateNotification(role, "User Created", Page.User.Identity.Name + " created the user named '" + user.UserName + "'.", DateTime.Now, "Info", null, null); // Save changes tgo db db.SaveChanges(); } else { lblMessage.Text = idResult.Errors.FirstOrDefault(); } } catch (DataException dx) { lblMessage.Text = "A data error occured. Please try again later or contact your Administrator if this continues to happen."; LogFile.WriteToFile("UserManager.aspx.cs", "btnCreateUser_Click", dx, "Data error when creating user", "HPSErrorLog.txt"); } catch (Exception ex) { lblMessage.Text = "An error occured. Please try again later or contact your Administrator if this continues to happen."; LogFile.WriteToFile("UserManager.aspx.cs", "btnCreateUser_Click", ex, "Error when creating user", "HPSErrorLog.txt"); } }
protected void Page_Load(object sender, EventArgs e) { ActivityTracker.Track("FitBit Manager", (int)UserActionEnum.Navigated); if (Page.User.Identity.IsAuthenticated && Session["UserId"] != null) { // Find the HPSUser that is currently logged in string userId = Session["UserId"].ToString(); // There are no session cookies for this user, callback and repopulate the session if (Session["FitbitAuthToken"] == null || Session["FitbitAuthTokenSecret"] == null || Session["FitbitUserId"] == null) { FitBit.FitBit.Callback(); } if (!IsPostBack) { try { HPSUser user = db.HPSUsers.Where(u => u.AspNetUser.Id == userId).SingleOrDefault(); // Check if the fitbituserId for this user has been set, if not set it if (user.FitBitUserId == null) { string fitBitUserId = Session["FitbitUserId"].ToString(); user.FitBitUserId = fitBitUserId; db.Entry(user).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } catch (DataException dx) { LogFile.WriteToFile("FitBitManager.aspx.cs", "Page_Load", dx, "The system failed when trying to automatically set the current user's FitBitId.", "HPSErrorLog.txt"); } catch (Exception ex) { LogFile.WriteToFile("FitBitManager.aspx.cs", "Page_Load", ex, "The system failed when trying to automatically set the current user's FitBitId.", "HPSErrorLog.txt"); } // Automatically Synchronize fitbit data and load the rest of the data UploadFitBitData(); // Draw initial chart DrawChart(this.Page, 7, "Steps"); } // Load step, minutes, and distance data GetStepGoals(); GetDistanceGoals(); GetMinuteGoals(); // Build tables for viewing all goals TableBuilder.BuildStepGoalsTable(tblStepGoals, userId); TableBuilder.BuildDistanceGoalsTable(tblDistanceGoals, userId); TableBuilder.BuildMinuteGoalsTable(tblMinuteGoals, userId); // Check if theres a notification if (notification) { lblCRUDMessage.Text = notificationMessage; lblCRUDMessage.CssClass = notificationStyle; notification = false; // Draw initial chart DrawChart(this.Page, 7, "Steps"); } } else { Response.Redirect("/Main.aspx"); } }