예제 #1
0
        public async Task <IActionResult> Create([Bind("Subject,Rating,Content,ReviewableId")] Review review)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

                if (currentUser == null)
                {
                    throw new Exception("Current user not found.");
                }

                review.Author   = currentUser;
                review.AuthorId = currentUser.Id;

                _context.Reviews.Add(review);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            //ViewData["AuthorId"] = new SelectList(_context.Users, "Id", "Id", review.AuthorId);
            ViewData["ReviewableId"] = (IEnumerable <SelectListItem>)BuildReviewablesDropDownList();

            return(View(await BuildItemReviewViewModelAsync(review)));
        }
        public async Task <IActionResult> CreateAsync([Bind("Id,Name")] IdentityRole role)
        {
            if (ModelState.IsValid)
            {
                await _roleManager.CreateAsync(role);

                await _dataContext.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(role));
        }
        public async Task <IActionResult> Create([Bind("Publisher,Name,ArtistName,ReleaseDate")] Album album)
        {
            ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (currentUser == null)
            {
                throw new Exception("Cannot find user.");
            }

            album.ArtistId = currentUser.Id;
            album.Artist   = currentUser;

            if (ModelState.IsValid)
            {
                _context.Add(album);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            //ViewData["ArtistId"] = new SelectList(_context.Users, "Id", "Id", album.ArtistId);
            return(View(album));
        }
        public async Task <IActionResult> CreateAsync([Bind("Name,ArtistName,Length,ReleaseDate,TrackNumber,Publisher,AlbumId")] Song song)
        {
            ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (ModelState.IsValid)
            {
                song.ArtistId = currentUser.Id;
                song.Artist   = currentUser;

                _context.Add(song);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Admin"));
            }

            ViewData["AlbumId"] = (IEnumerable <SelectListItem>)BuildUserAlbumsDropDownList(currentUser);
            return(View(song));
        }
        //[Authorize(Roles = "Admin")]
        public async Task <IActionResult> CreateAsync([Bind("UserName,LastName,FirstName,Email,Password,ConfirmPassword,Age,Gender")] CreateUserViewModel user)
        {
            string userName = User.Identity.Name;

            if (userName == null)
            {
                return(StatusCode(400));
            }

            // ensure user's role is admin
            ApplicationUser currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

            if (!await _userManager.IsInRoleAsync(currentUser, "Admin"))
            {
                return(RedirectToAction("AccessDenied", "Error"));
            }

            // validate user's authority to create user
            // only admin allowed
            // ApplicationUser currentUser = await _userManager.FindByNameAsync(userName);
            // if (await _userManager.IsInRoleAsync(currentUser, "Admin")) {}
            // else
            // {
            //  return RedirectToAction("AccessDenied", "Error");
            // }

            if (ModelState.IsValid)
            {
                var newUser = new ApplicationUser()
                {
                    UserName  = user.UserName,
                    FirstName = user.FirstName,
                    LastName  = user.LastName,
                    Email     = user.Email,
                    Age       = user.Age,
                    Gender    = user.Gender
                };

                var result = await _userManager.CreateAsync(newUser);

                if (result.Succeeded)
                {
                    PasswordHasher <ApplicationUser> ph = new PasswordHasher <ApplicationUser>();
                    newUser.PasswordHash = ph.HashPassword(newUser, user.Password);
                    await _dataContext.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }

                AddErrors(result);
            }

            return(View(user));
        }