1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Added automatic Database Migration on startup.

Moved Models and ViewModels into their respective Areas.
Added Groups/Roles to models.
This commit is contained in:
Uncled1023 2015-11-24 16:52:20 -08:00
parent 5d3c502d74
commit 039fe574d6
29 changed files with 337 additions and 220 deletions

View File

@ -30,7 +30,7 @@ namespace Teknik
bundles.Add(new ScriptBundle("~/bundles/common").Include(
"~/Scripts/common.js"));
bundles.Add(new StyleBundle("~/Content/css").Include(
bundles.Add(new StyleBundle("~/Content/CSS/Common").Include(
"~/Content/CSS/bootstrap.css",
"~/Content/CSS/font-awesome.css",
"~/Content/CSS/Site.css"));

View File

@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using Teknik.Areas.Profile.Models;
namespace Teknik.Models
namespace Teknik.Areas.Blog.Models
{
public class Blog
{

View File

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Teknik.Models
namespace Teknik.Areas.Blog.Models
{
public class Post
{

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Teknik.Areas.Blog.Models;
using Teknik.Controllers;
using Teknik.Models;

View File

@ -1,4 +1,4 @@
@using Teknik.Models
@using Teknik.Areas.Blog.Models
<div class="container">
<div class="row">
<center>

View File

@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Teknik.Areas.Profile.ViewModels;
using Teknik.Controllers;
using Teknik.Models;
using Teknik.ViewModels;
namespace Teknik.Areas.Profile.Controllers
{
public class ProfileController : DefaultController
{
private TeknikEntities db = new TeknikEntities();
// GET: Profile/Profile
public ActionResult Index()
{
ViewBag.Title = Config.Title + " - Profile";
ViewBag.Message = "View Your Profile";
return View();
}
[HttpGet]
[AllowAnonymous]
// GET: Profile
public ActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
if (model.IsValid())
{
FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
return Json(new { result = "true" });
}
}
return Json(new { error = "Invalid User name or Password." });
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home", new { Area = "Home" });
}
[HttpPost]
[AllowAnonymous]
public ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var foundUser = db.Users.Where(b => b.Username == model.Username);
if (foundUser.Any())
{
return Json(new { error = "That username already exists." });
}
if (model.Password != model.ConfirmPassword)
{
return Json(new { error = "Passwords must match." });
}
if (model.Insert())
{
return RedirectToAction("Login", "Profile", new LoginViewModel { Username = model.Username, Password = model.Password });
}
return Json(new { error = "You must include all fields." });
}
return Json(new { error = "You must include all fields." });
}
}
}

View File

@ -0,0 +1,14 @@
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teknik.Areas.Profile.Models
{
public class AuthUser : IdentityUser
{
public User User { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teknik.Areas.Profile.Models
{
public class Group
{
public int GroupId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List<Role> Roles { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teknik.Areas.Profile.Models
{
public enum PermissionTarget
{
Blog,
User,
Profile
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teknik.Areas.Profile.Models
{
public enum PermissionType
{
Add,
Edit,
Delete
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teknik.Areas.Profile.Models
{
public class Role
{
public int RoleId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public PermissionType Permission { get; set; }
public PermissionTarget Target { get; set; }
}
}

View File

@ -1,8 +1,9 @@
using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Collections.Generic;
namespace Teknik.Models
namespace Teknik.Areas.Profile.Models
{
public class User
{
@ -16,12 +17,15 @@ namespace Teknik.Models
public DateTime LastSeen { get; set; }
public List<Group> Groups { get; set; }
public User()
{
Username = String.Empty;
HashedPassword = String.Empty;
JoinDate = DateTime.Now;
LastSeen = DateTime.Now;
Groups = new List<Group>();
}
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teknik.Areas.Profile.Models
{
public class UserIdentity : User
{
}
}

View File

@ -0,0 +1,33 @@
using System.Web.Mvc;
namespace Teknik.Areas.Profile
{
public class ProfileAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Profile";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapSubdomainRoute(
"Profile_dev", // Route name
"dev",
"Profile/{controller}/{action}", // URL with parameters
new { controller = "Profile", action = "Index" }, // Parameter defaults
new[] { typeof(Controllers.ProfileController).Namespace }
);
context.MapSubdomainRoute(
"Profile_default", // Route name
"profile",
"{controller}/{action}", // URL with parameters
new { controller = "Profile", action = "Index" }, // Parameter defaults
new[] { typeof(Controllers.ProfileController).Namespace }
);
}
}
}

View File

@ -7,7 +7,7 @@ using System.Web.Security;
using Teknik.Helpers;
using Teknik.Models;
namespace Teknik.ViewModels
namespace Teknik.Areas.Profile.ViewModels
{
public class LoginViewModel
{

View File

@ -1,9 +1,10 @@
using System;
using System.ComponentModel.DataAnnotations;
using Teknik.Areas.Profile.Models;
using Teknik.Helpers;
using Teknik.Models;
namespace Teknik.ViewModels
namespace Teknik.Areas.Profile.ViewModels
{
public class RegisterViewModel
{

View File

@ -0,0 +1 @@
@using Teknik.Models

View File

@ -1,6 +1,6 @@
@model Teknik.ViewModels.LoginViewModel
@model Teknik.Areas.Profile.ViewModels.LoginViewModel
<form role="form" id="loginForm" action="@Url.Action("Login", "Profile", new { area = "Home" })" method="post" accept-charset="UTF-8">
<form role="form" id="loginForm" action="@Url.Action("Login", "Profile", new { area = "Profile" })" method="post" accept-charset="UTF-8">
@Html.ValidationSummary(true, "Login failed. Check your login details.")
<div class="form-group">
<input type="text" class="form-control" id="Username" value="" placeholder="Username" name="Username" data-val-required="The Username field is required." data-val="true" />

View File

@ -1,4 +1,4 @@
@model Teknik.ViewModels.RegisterViewModel
@model Teknik.Areas.Profile.ViewModels.RegisterViewModel
<form role="form" id="registrationForm" action="@Url.Action("Register", "Profile", new { area = "Profile" })" method="post" accept-charset="UTF-8">
@Html.ValidationSummary(true, "Registration failed. Check your registration details.")

View File

@ -0,0 +1,3 @@
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}

View File

@ -0,0 +1,36 @@
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Optimization" />
<add namespace="Teknik" />
</namespaces>
</pages>
</system.web.webPages.razor>
<appSettings>
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.webServer>
<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>

View File

@ -1,193 +0,0 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Security.Policy;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using SecurityDriven.Inferno.Hash;
using SecurityDriven.Inferno.Mac;
using Teknik.Models;
using Teknik.ViewModels;
using SHA384 = Teknik.Helpers.SHA384;
namespace Teknik.Controllers
{
public class ProfileController : DefaultController
{
private TeknikEntities db = new TeknikEntities();
[AllowAnonymous]
// GET: Profile
public ActionResult Index()
{
return View();
}
[HttpGet]
[AllowAnonymous]
// GET: Profile
public ActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
if (model.IsValid())
{
FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
return Json(new {result = "true"});
}
}
return Json(new { error = "Invalid User name or Password." });
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Home");
}
[HttpPost]
[AllowAnonymous]
public ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var foundUser = db.Users.Where(b => b.Username == model.Username);
if (foundUser.Any())
{
return Json(new {error = "That username already exists."});
}
if (model.Password != model.ConfirmPassword)
{
return Json(new {error = "Passwords must match."});
}
if (model.Insert())
{
return RedirectToAction("Login", "Profile", new LoginViewModel { Username = model.Username, Password = model.Password });
}
return Json(new { error = "You must include all fields." });
}
return Json(new { error = "You must include all fields." });
}
// GET: Profile/Details/5
public ActionResult Details(int? id)
{
if (ModelState.IsValid)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
return HttpNotFound();
}
// GET: Profile/Create
public ActionResult Create()
{
return View();
}
// POST: Profile/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "UserId,Username")] User user)
{
if (ModelState.IsValid)
{
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
// GET: Profile/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// POST: Profile/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "UserId,Username")] User user)
{
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
// GET: Profile/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
return View(user);
}
// POST: Profile/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
User user = db.Users.Find(id);
db.Users.Remove(user);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}

View File

@ -8,6 +8,8 @@ using System.Web.Routing;
using Teknik.Models;
using System.Data.Entity;
using System.Web.Security;
using Teknik.Migrations;
using System.Data.Entity.Migrations;
namespace Teknik
{
@ -15,6 +17,8 @@ namespace Teknik
{
protected void Application_Start()
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<TeknikEntities, Configuration>());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

View File

@ -0,0 +1,32 @@
namespace Teknik.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<Teknik.Models.TeknikEntities>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(Teknik.Models.TeknikEntities context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
}

View File

@ -1,17 +1,25 @@
using System.Data.Entity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using Teknik.Areas.Blog.Models;
using Teknik.Areas.Profile.Models;
using Teknik.Migrations;
namespace Teknik.Models
{
public class TeknikEntities : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Group>().ToTable("Groups");
modelBuilder.Entity<Role>().ToTable("Roles");
modelBuilder.Entity<Blog>().ToTable("Blogs");
modelBuilder.Entity<Post>().ToTable("Posts");

View File

@ -145,21 +145,29 @@
<Compile Include="Areas\Home\HomeAreaRegistration.cs" />
<Compile Include="Areas\Privacy\Controllers\PrivacyController.cs" />
<Compile Include="Areas\Privacy\PrivacyAreaRegistration.cs" />
<Compile Include="Areas\Profile\Controllers\ProfileController.cs" />
<Compile Include="Areas\Profile\Models\AuthUser.cs" />
<Compile Include="Areas\Profile\Models\UserIdentity.cs" />
<Compile Include="Areas\Profile\ProfileAreaRegistration.cs" />
<Compile Include="Configuration\Config.cs" />
<Compile Include="Areas\Blog\Controllers\BlogController.cs" />
<Compile Include="Controllers\DefaultController.cs" />
<Compile Include="Areas\Dev\Controllers\DevController.cs" />
<Compile Include="Controllers\ProfileController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Helpers\Crypto.cs" />
<Compile Include="Models\Post.cs" />
<Compile Include="Areas\Profile\Models\Group.cs" />
<Compile Include="Areas\Profile\Models\PermissionTarget.cs" />
<Compile Include="Areas\Profile\Models\PermissionType.cs" />
<Compile Include="Areas\Blog\Models\Post.cs" />
<Compile Include="Areas\Profile\Models\Role.cs" />
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Models\TeknikEntities.cs" />
<Compile Include="Models\User.cs" />
<Compile Include="Models\Blog.cs" />
<Compile Include="ViewModels\LoginViewModel.cs" />
<Compile Include="ViewModels\RegisterViewModel.cs" />
<Compile Include="Areas\Profile\Models\User.cs" />
<Compile Include="Areas\Blog\Models\Blog.cs" />
<Compile Include="Areas\Profile\ViewModels\LoginViewModel.cs" />
<Compile Include="Areas\Profile\ViewModels\RegisterViewModel.cs" />
<Compile Include="ViewModels\ViewModelBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="App_Start\SubdomainRoute.cs" />
@ -214,6 +222,9 @@
<Content Include="Areas\Contact\Views\web.config" />
<Content Include="Areas\Contact\Views\_ViewStart.cshtml" />
<Content Include="Areas\Contact\Views\Contact\Index.cshtml" />
<Content Include="Areas\Profile\Views\web.config" />
<Content Include="Areas\Profile\Views\_ViewStart.cshtml" />
<Content Include="Areas\Profile\Views\Profile\Index.cshtml" />
<None Include="Properties\PublishProfiles\Teknik Dev.pubxml" />
<None Include="Scripts\jquery-2.1.4.intellisense.js" />
<Content Include="Scripts\common.js" />
@ -250,15 +261,13 @@
<Content Include="Views\Shared\_LoginPartial.cshtml" />
<Content Include="Views\Shared\_Navbar.cshtml" />
<Content Include="Views\Shared\_Footer.cshtml" />
<Content Include="Views\Profile\Index.cshtml" />
<Content Include="Views\Profile\Login.cshtml" />
<Content Include="Views\Profile\Register.cshtml" />
<Content Include="Areas\Profile\Views\Profile\Login.cshtml" />
<Content Include="Areas\Profile\Views\Profile\Register.cshtml" />
<Content Include="Areas\Dev\Views\Dev\Index.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="Areas\About\Models\" />
<Folder Include="Areas\About\Views\Shared\" />
<Folder Include="Areas\Blog\Models\" />
<Folder Include="Areas\Blog\Views\Shared\" />
<Folder Include="Areas\Contact\Models\" />
<Folder Include="Areas\Contact\Views\Shared\" />
@ -268,6 +277,7 @@
<Folder Include="Areas\Home\Views\Shared\" />
<Folder Include="Areas\Privacy\Models\" />
<Folder Include="Areas\Privacy\Views\Shared\" />
<Folder Include="Areas\Profile\Views\Shared\" />
</ItemGroup>
<ItemGroup>
<Content Include="packages.config" />

View File

@ -1 +0,0 @@


View File

@ -8,7 +8,7 @@
<meta name="author" content="@ViewBag.Config.Author" />
<title>@ViewBag.Title</title>
<link rel="shortcut icon" href="/Content/Images/favicon.ico" type="image/x-icon" />
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/CSS/Common")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/markdown")

View File

@ -12,14 +12,14 @@
<li>
<a href="@Url.Action("Index", "Blog", new { area = "Blog" })/@User.Identity.Name">Blog</a>
</li>
if (User.Identity.Group == Groups.Founder)
@if (User.IsInRole("Admin"))
{
<li>
<a href="@Url.Action("Index", "Admin", new { area = "Admin" })">Administration</a>
</li>
}
<li>
@Html.ActionLink("Sign Out", "Logout", "Profile", new { area = "Profile" })
@Html.ActionLink("Sign Out", "Logout", "Profile", new { area = "Profile" }, null)
</li>
</ul>
</li>
@ -29,14 +29,14 @@ else
<li class="dropdown">
<a class="dropdown-toggle" href="#" data-toggle="dropdown" id="reg_dropdown">Sign Up <strong class="caret"></strong></a>
<div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
@Html.Partial("../Profile/Register", new Teknik.ViewModels.RegisterViewModel())
@Html.Partial("../../Areas/Profile/Views/Profile/Register", new Teknik.Areas.Profile.ViewModels.RegisterViewModel())
</div>
</li>
<li class="dropdown">
<a class="dropdown-toggle" href="#" data-toggle="dropdown" id="login_dropdown">Sign In <strong class="caret"></strong></a>
<div class="dropdown-menu" style="padding: 15px; padding-bottom: 0px;">
@Html.Partial("../Profile/Login", new Teknik.ViewModels.LoginViewModel())
@Html.Partial("../../Areas/Profile/Views/Profile/Login", new Teknik.Areas.Profile.ViewModels.LoginViewModel())
</div>
</li>
}