Пример #1
0
        public IHttpActionResult DeleteUser(int id)
        {
            // Allow only for authorized user
            var userToCheck = _userRepository.FirstOrDefault(u => u.UserName == User.Identity.Name);

            if (!userToCheck.Authorized)
            {
                return(Unauthorized());
            }

            // Get the DB user corresponding to the user ID
            User dbUser = _userRepository.GetByID(id);

            if (dbUser == null)
            {
                return(NotFound());
            }

            // Remove any external logins corresponding to user
            var dbExternalLogins = _externalLoginRepository.Where(el => el.UserID == id);

            if (dbExternalLogins.Count() > 0)
            {
                foreach (var dbExternalLogin in dbExternalLogins)
                {
                    _externalLoginRepository.Delete(dbExternalLogin);
                }
            }

            // Remove the user
            _userRepository.Delete(dbUser);

            try
            {
                _unitOfWork.Commit();
            }
            catch (Exception e)
            {
                throw new Exception("Unable to delete the user from the database", e);
            }

            return(Ok(Mapper.Map <UserModel>(dbUser)));
        }