/// <summary> /// Change the info of the user /// </summary> /// <param name="info">The new info of the user</param> /// See <see cref="Areas.UserInfo.Models.ChangeUserInfo"/> to know the param structure /// <returns>IActionResult of the change user info action</returns> /// See <see cref="Areas.UserInfo.Models.UserData"/> to know the response structure public IActionResult changeUser([FromBody] ChangeUserInfo info) { User user = TokenUserManager.getUserFromToken(HttpContext, _context); if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } bool isAdmin = AdminPolicy.isAdmin(user, _context); try { user.nickname = !isAdmin?changeNickname(info.nickname, user.nickname) : user.nickname; user.password = changePassword(info.oldpassword, info.newPassword, user.password); user.profileImg = !isAdmin ? info.image ?? user.profileImg : user.profileImg; _context.Update(user); _context.SaveChanges(); } catch (DbUpdateException) { return(StatusCode(500)); } catch (Exception e) { if (e.Message == "") { return(BadRequest()); } else { return(BadRequest(new { error = e.Message })); } } string successRes = ""; if (_changePass) { successRes = "PassChanged"; } UserData userShow = new UserData { email = user.email, nickname = user.nickname, img = user.profileImg, groups = GroupsOfUser.get(user, _context), timeSignUp = user.dateSignUp }; return(Ok(new { success = successRes, info = userShow })); }
// // ────────────────────────────────────────────────────────────────────────────────── // :::::: P U B L I C F U N C T I O N S : : : : : : : : // ────────────────────────────────────────────────────────────────────────────────── // /// <summary> /// Make the usersession object for a user /// </summary> /// <param name="context">The database context</param> /// <param name="user">The user who wants the session </param> /// <param name="provider">The provider of the caller</param> /// <returns>The session of the user</returns> /// See <see cref="Areas.Identity.Models.UserSession"/> to know the response structure public static UserSession getUserSession(ApplicationDBContext context, User user, Boolean provider) { try { UserSession session = getUserJson(context, user, provider); if (session != null) { List <string> groups = GroupsOfUser.get(user, context); session.groups = groups; context.SaveChanges(); return(session); } return(null); } catch (Exception) { return(null); } }
/// <summary> /// Get the user profile /// </summary> /// <returns>The IActionResult of the get user action</returns> /// See <see cref="Areas.UserInfo.Models.UserData"/> to know the response structure public IActionResult getUser() { try { User user = TokenUserManager.getUserFromToken(HttpContext, _context); if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } _context.Entry(user).Reference("role").Load(); UserData userShow = new UserData { email = user.email, nickname = user.nickname, img = user.profileImg, groups = GroupsOfUser.get(user, _context), timeSignUp = user.dateSignUp }; return(Ok(userShow)); } catch (Exception) { return(StatusCode(500)); } }