예제 #1
0
        public IActionResult Join(JoinGroupModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(View("Join", model));
            }

            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

            if (!_groupsService.GroupExists(model.Id) || _groupsService.IsMember(model.Id, userId))
            {
                return(BadRequest());
            }

            //Check if RequirePassword wasnt chnged by a user
            if (!model.RequirePassword && _groupsService.PasswordRequired(model.Id))
            {
                return(BadRequest());
            }

            if (model.RequirePassword)
            {
                if (!_groupsService.CheckPassword(model.Id, model.Password))
                {
                    ModelState.AddModelError("Password", "Wrong password");
                    return(View("Join", model));
                }

                _groupsService.AddUserToGroup(userId, model.Id);
                return(RedirectToAction("Details", new { id = model.Id }));
            }

            _groupsService.AddUserToGroup(userId, model.Id);

            return(RedirectToAction("Details", new { id = model.Id }));
        }