public void AddUserToGroup(string userId, int groupId) { Group group = _db.Groups.Find(groupId); ApplicationUser user = _db.Users.Find(userId); var userGroup = new ApplicationUserGroup { Group = group, GroupId = group.Id, User = user, UserId = user.Id }; foreach (ApplicationRoleGroup role in group.Roles) { _userManager.AddToRoleAsync(user, role.Role.Name); } user.Groups.Add(userGroup); _db.SaveChanges(); }
public List <ApplicationUserGroup> PopulateUserGroups(string groupId) { var userGroups = new List <ApplicationUserGroup>(); using (var conn = new MySqlConnection(_connectionString)) { var parameters = new Dictionary <string, object> { { "@groupId", groupId } }; var reader = MySqlHelper.ExecuteReader(conn, CommandType.Text, @"SELECT ApplicationUserId,ApplicationGroupId FROM applicationusergroups WHERE ApplicationGroupId = @groupId", parameters); while (reader.Read()) { var userGroup = new ApplicationUserGroup(); userGroup.ApplicationUserId = reader[0].ToString(); userGroup.ApplicationGroupId = reader[1].ToString(); userGroups.Add(userGroup); } } return(userGroups); }
public async Task <ActionResult> JoinGroup(MembersViewModel viewModel, int groupId) { var userInDb = await _context.Users.Include(u => u.ApplicationUserGroups).SingleOrDefaultAsync(u => u.Id == viewModel.Users.Id); var groupInDb = await _context.Groups.SingleOrDefaultAsync(g => g.Id == groupId); // Notify group creator when a new member joins their group if (groupInDb.GroupTypesId == 1) // send join notification if it's an open group { var notification = new Notification() { ApplicationUserId = groupInDb.ApplicationUserId, Date = DateTime.Now, Message = userInDb.FirstName + " " + userInDb.LastName + " joined " + groupInDb.Name, NotificationType = "Plain", MemberId = viewModel.Users.Id, GroupId = groupId }; //Increase the number of members each time a user joins a group groupInDb.NumberOfMembers++; _context.Notifications.Add(notification); } if (groupInDb.GroupTypesId == 2) // send request notification if it's a close group { var notification = new Notification() { ApplicationUserId = groupInDb.ApplicationUserId, Date = DateTime.Now, Message = userInDb.FirstName + " " + userInDb.LastName + " requested to join " + groupInDb.Name, NotificationType = "Request", MemberId = viewModel.Users.Id, GroupId = groupId }; _context.Notifications.Add(notification); } viewModel = new MembersViewModel() { Users = await _context.Users.SingleOrDefaultAsync(u => u.Id == viewModel.Users.Id), Groups = await _context.Groups.Include(g => g.GroupTypes).Include(g => g.EntryTypes).Include(g => g.Interests).ToListAsync() }; // Redirect user if user has already joined the group var userGroups = await _context.ApplicationUserGroups.ToListAsync(); foreach (var item in userGroups) { if ((item.ApplicationUserId == viewModel.Users.Id) && (item.GroupId == groupId)) { ViewBag.alreadyJoined = true; ViewBag.joinSuccess = false; ViewBag.leaveSuccess = false; return(View("MembersHome", viewModel)); } } //create an instance of ApplicationUserGroup ApplicationUserGroup appUserGroup = new ApplicationUserGroup { ApplicationUserId = viewModel.Users.Id, GroupId = groupId }; // Save group if group type is "open" if (groupInDb.GroupTypesId == 1) { _context.ApplicationUserGroups.Add(appUserGroup); } // Redirect to payment page for paid groups if (groupInDb.EntryTypes.Name == "Paid") { return(RedirectToAction("Payment", new { customerEmail = viewModel.Users.Email, firstName = viewModel.Users.FirstName, lastName = viewModel.Users.LastName })); } await _context.SaveChangesAsync(); return(RedirectToAction("Index", new { joinSuccess = true })); }
public async Task Add(ApplicationUserGroup userGroupRelation) { _context.Add(userGroupRelation); await _context.SaveChangesAsync(); }
public async Task <IHttpActionResult> Register([FromBody] RegisterBindingModel model) { Response response = new Response(); //UserInfo checkUser = Service.userInfoDAO.checkExist("identityNumber", model.identityNumber); //if (checkUser != null) //{ // response.code = "409"; // response.status = "Số chứng minh nhân dân đã được đăng ký"; // return Content<Response>(HttpStatusCode.Conflict, response); //} var identityUser = new ApplicationUser() { UserName = model.Email, Email = model.Email, PhoneNumber = model.PhoneNumber }; identityUser.UserInfo = new UserInfo(0, model.firstName, model.lastName, model.dob, model.identityNumber); IdentityResult result = new IdentityResult(); try { result = await Service._userManager.CreateAsync(identityUser, model.Password); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.ToString()); } if (!result.Succeeded) { var message = "Đăng ký thất bại. Xin vui lòng đăng ky lại!"; response.code = "400"; response.status = "Đăng ký thất bại"; response.results = message; return(Content <Response>(HttpStatusCode.BadRequest, response)); } else { model.groups = new List <string>(); if (model.groupName == "Merchant") { model.groups.Add("Customer"); model.groups.Add("Merchant"); } else { model.groups.Add(model.groupName); } foreach (var group in model.groups) { Group gr = Service.groupDAO.getGroupByName(group); ApplicationUserGroup userGroup = new ApplicationUserGroup(); userGroup.groupId = gr.id; userGroup.userId = identityUser.Id; Service.userGroupDAO.AddUserToGroup(userGroup); } string code = await Service._userManager.GenerateEmailConfirmationTokenAsync(identityUser.Id); var callbackUrl = new Uri(Url.Link("ConfirmEmail", new { userId = identityUser.Id, code = code })); await Service._userManager.SendEmailAsync(identityUser.Id, "Xác thực tài khoản của bạn", "Vui lòng nhấn vào link sau: <a href=\"" + callbackUrl + "\">link</a>"); var message = "Chúng tôi đã gửi email xác thực tài khoản vào mail " + identityUser.Email + " . Vui lòng kiểm tra email để xác thực."; response.code = "201"; response.status = "Đăng ký thành công. " + message; response.results = identityUser; return(Content <Response>(HttpStatusCode.Created, response)); } }
public async Task <ActionResult> Add([Bind(Include = "ApplicationUserId, ApplicationGroupId")] ApplicationUserGroup userGroup, params string[] selectedGroups) { await GroupManager.SetUserGroupAsync(userGroup.ApplicationUserId, userGroup.ApplicationGroupId); return(Json(new { Success = true })); }