示例#1
0
        public IHttpActionResult SetOwner(SetRoleBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = _context.Users.SingleOrDefault(u => u.Id == model.AccountId);

            if (user == null)
            {
                return(BadRequest("The user have account id " + model.AccountId + " does not exist."));
            }

            var owner = _context.Owners.SingleOrDefault(o => o.AccountId == user.Id);

            if (owner == null)
            {
                return(BadRequest("The user have account id " + model.AccountId + " is not an Owner."));
            }

            var listRole = _userManager.GetRoles(user.Id);

            if (listRole.Count > 0)
            {
                _userManager.RemoveFromRoles(user.Id, listRole.ToArray());
            }
            _userManager.AddToRole(user.Id, RoleName.Owner);

            return(Ok("Approved"));
        }
示例#2
0
        public IHttpActionResult RejectOwner(SetRoleBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = _context.Users.SingleOrDefault(u => u.Id == model.AccountId);

            if (user == null)
            {
                return(BadRequest("The user have account id " + model.AccountId + " does not exist."));
            }

            var owner = _context.Owners.SingleOrDefault(o => o.AccountId == user.Id);

            if (owner == null)
            {
                return(BadRequest("The user have account id " + model.AccountId + " is not an Owner."));
            }

            if (!_userManager.IsInRole(model.AccountId, RoleName.WaitForConfirmation))
            {
                return(BadRequest("User has been approved."));
            }

            _context.Owners.Remove(owner);
            _context.SaveChanges();
            _userManager.Delete(user);
            _context.SaveChanges();

            return(Ok("Rejected"));
        }
示例#3
0
        public async Task <IHttpActionResult> SetRole(SetRoleBindingModel model)
        {
            if (model.UserId == null)
            {
                model.UserId = User.Identity.GetUserId();
                Validate(model);
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            IdentityResult result;

            if (await UserManager.IsInRoleAsync(model.UserId, model.Role))
            {
                result = await UserManager.RemoveFromRoleAsync(model.UserId, model.Role);
            }
            else
            {
                result = await UserManager.AddToRoleAsync(model.UserId, model.Role);
            }

            if (!result.Succeeded)
            {
                return(GetErrorResult(result));
            }

            return(Ok());
        }