/// <summary> /// Function to import new users from Auth0 /// </summary> /// <returns></returns> public bool ImportUsersFromAuth0() { try { using (var db = new TicketingSystemDBContext()) { var users = Auth0APIClient.GetAllUsers(); foreach (var u in users) { if (!db.Users.Where(usr => usr.Email == u.email).Any()) { string shiftType = Auth0APIClient.GetUserRole(u.user_id)[0].name; Users newUser = new Users(); newUser.Auth0Uid = u.user_id; newUser.FullName = u.name; newUser.Email = u.email; newUser.ShiftType = shiftType; CreateAndAddUser(newUser); } } } return(true); } catch (Exception e) { throw new HttpResponseException(Utility.CreateResponseMessage(e)); } }
/// <summary> /// Function to update users in the database from Auth0 /// </summary> /// <returns></returns> public bool UpdateUsersFromAuth0() { try { using (var db = new TicketingSystemDBContext()) { var users = Auth0APIClient.GetAllUsers(); foreach (var user in users) { Users dbuser = db.Users.Where(u => u.Auth0Uid == user.user_id).FirstOrDefault(); if (dbuser != null) { dbuser.Email = user.email; dbuser.FullName = user.name; var roles = Auth0APIClient.GetUserRole(dbuser.Auth0Uid); dbuser.ShiftType = roles.ElementAt(0).name; db.Users.Update(dbuser); db.SaveChanges(); } } } return(true); } catch (Exception e) { throw new HttpResponseException(Utility.CreateResponseMessage(e)); } }
/// <summary> /// Function to create a new user in the databse and add the user to Auth0 /// </summary> /// <param name="newUser"></param> /// <param name="loggedInUser"></param> /// <returns></returns> public bool CreateUser(Users newUser, UserData loggedInUser) { try { string tempPass = CreateAndAddUser(newUser); if (newUser.ShiftType != "Warehouse") { if (tempPass.Equals("Email Exists")) { return(false); } string auth0ID = Auth0APIClient.AddUser(newUser, tempPass); Auth0APIClient.SetRole(auth0ID, newUser.ShiftType); } return(true); } catch (Exception e) { throw new HttpResponseException(Utility.CreateResponseMessage(e)); } }