Skip to content

Commit

Permalink
Update seeder admin
Browse files Browse the repository at this point in the history
  • Loading branch information
fmiqbal committed Jun 8, 2020
1 parent 2ec51dd commit ad6477a
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 91 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
bin/
publish/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
/_ReSharper.Caches/
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"appService.defaultWebAppToDeploy": "/subscriptions/281a4f2a-ea4f-4ec6-8a66-be5fc2a70437/resourceGroups/Xtremax/providers/Microsoft.Web/sites/facade-voting",
"appService.deploySubpath": "publish"
}
16 changes: 14 additions & 2 deletions Areas/Identity/IdentityHostingStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,31 @@
using Microsoft.Extensions.DependencyInjection;

[assembly: HostingStartup(typeof(App.Areas.Identity.IdentityHostingStartup))]

namespace App.Areas.Identity
{
public class IdentityHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
builder.ConfigureServices((context, services) => {
builder.ConfigureServices((context, services) =>
{
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseSqlServer(
context.Configuration.GetConnectionString("AppIdentityDbContextConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>();
services.Configure<IdentityOptions>(options =>
{
options.SignIn.RequireConfirmedAccount = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequiredUniqueChars = 0;
options.Password.RequiredLength = 8;
});
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions Areas/Identity/Pages/Account/Register.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using App.Enum;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -75,6 +76,7 @@ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
if (ModelState.IsValid)
{
var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };
await _userManager.AddToRoleAsync(user, Role.User);
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
Expand Down
59 changes: 18 additions & 41 deletions Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using App.Data;
using App.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace App.Controllers
{
[Authorize(Policy = "RequireAdmin")]
public class CategoriesController : Controller
{
private readonly ApplicationDbContext _context;
Expand All @@ -28,17 +27,11 @@ public async Task<IActionResult> Index()
// GET: Categories/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
if (id == null) return NotFound();

var category = await _context.Category
.FirstOrDefaultAsync(m => m.Id == id);
if (category == null)
{
return NotFound();
}
if (category == null) return NotFound();

return View(category);
}
Expand All @@ -62,22 +55,17 @@ public async Task<IActionResult> Create([Bind("Id,Title")] Category category)
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}

return View(category);
}

// GET: Categories/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
if (id == null) return NotFound();

var category = await _context.Category.FindAsync(id);
if (category == null)
{
return NotFound();
}
if (category == null) return NotFound();
return View(category);
}

Expand All @@ -88,10 +76,7 @@ public async Task<IActionResult> Edit(int? id)
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Title")] Category category)
{
if (id != category.Id)
{
return NotFound();
}
if (id != category.Id) return NotFound();

if (ModelState.IsValid)
{
Expand All @@ -103,39 +88,31 @@ public async Task<IActionResult> Edit(int id, [Bind("Id,Title")] Category catego
catch (DbUpdateConcurrencyException)
{
if (!CategoryExists(category.Id))
{
return NotFound();
}
else
{
throw;
}
throw;
}

return RedirectToAction(nameof(Index));
}

return View(category);
}

// GET: Categories/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
if (id == null) return NotFound();

var category = await _context.Category
.FirstOrDefaultAsync(m => m.Id == id);
if (category == null)
{
return NotFound();
}
if (category == null) return NotFound();

return View(category);
}

// POST: Categories/Delete/5
[HttpPost, ActionName("Delete")]
[HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
Expand All @@ -150,4 +127,4 @@ private bool CategoryExists(int id)
return _context.Category.Any(e => e.Id == id);
}
}
}
}
62 changes: 17 additions & 45 deletions Controllers/FeaturesController.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using App.Data;
using App.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using App.Data;
using App.Models;
using Microsoft.VisualBasic.CompilerServices;

namespace App.Controllers
{
[Authorize(Policy = "RequireAdmin")]
public class FeaturesController : Controller
{
private readonly ApplicationDbContext _context;
Expand All @@ -26,10 +26,7 @@ public async Task<IActionResult> Index(int categoryId)
ViewData["Categories"] = new SelectList(_context.Category, "Id", "Title");
var features = from f in _context.Feature select f;

if (categoryId != 0)
{
features = features.Where(s => s.CategoryId == categoryId);
}
if (categoryId != 0) features = features.Where(s => s.CategoryId == categoryId);

return View(await features
.Include(m => m.Category)
Expand All @@ -39,18 +36,12 @@ public async Task<IActionResult> Index(int categoryId)
// GET: Features/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
if (id == null) return NotFound();

var feature = await _context.Feature
.Include(m => m.Category)
.FirstOrDefaultAsync(m => m.Id == id);
if (feature == null)
{
return NotFound();
}
if (feature == null) return NotFound();

return View(feature);
}
Expand Down Expand Up @@ -79,22 +70,16 @@ public async Task<IActionResult> Create([Bind("Id,Title,Description,DueDate,Cate
return RedirectToAction(nameof(Index));
}

return this.Create();
return Create();
}

// GET: Features/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
if (id == null) return NotFound();

var feature = await _context.Feature.FindAsync(id);
if (feature == null)
{
return NotFound();
}
if (feature == null) return NotFound();

ViewData["Categories"] = new SelectList(_context.Category, "Id", "Title");
return View(feature);
Expand All @@ -108,10 +93,7 @@ public async Task<IActionResult> Edit(int? id)
public async Task<IActionResult> Edit(int id, [Bind("Id,Title,Description,DueDate,CategoryId")]
Feature feature)
{
if (id != feature.Id)
{
return NotFound();
}
if (id != feature.Id) return NotFound();

if (ModelState.IsValid)
{
Expand All @@ -123,41 +105,31 @@ public async Task<IActionResult> Edit(int id, [Bind("Id,Title,Description,DueDat
catch (DbUpdateConcurrencyException)
{
if (!FeatureExists(feature.Id))
{
return NotFound();
}
else
{
throw;
}
throw;
}

return RedirectToAction(nameof(Index));
}

return await this.Edit(id);
return await Edit(id);
}

// GET: Features/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
if (id == null) return NotFound();

var feature = await _context.Feature
.FirstOrDefaultAsync(m => m.Id == id);
if (feature == null)
{
return NotFound();
}
if (feature == null) return NotFound();

return View(feature);
}

// POST: Features/Delete/5
[HttpPost, ActionName("Delete")]
[HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
Expand Down
27 changes: 27 additions & 0 deletions Data/IdentityDbInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using App.Enum;
using Microsoft.AspNetCore.Identity;

namespace app.Data
{
public class IdentityDbInitializer
{
public static void SeedUsers(UserManager<IdentityUser> userManager)
{
if (userManager.FindByEmailAsync("gamer.fikri@gmail.com").Result == null)
{
IdentityUser user = new IdentityUser
{
UserName = "gamer.fikri@gmail.com",
Email = "gamer.fikri@gmail.com"
};

IdentityResult result = userManager.CreateAsync(user, ":C1nt4:Fu-kun;").Result;

if (result.Succeeded)
{
userManager.AddToRoleAsync(user, Role.Admin).Wait();
}
}
}
}
}
Loading

0 comments on commit ad6477a

Please sign in to comment.