create admin

This commit is contained in:
tmont 2011-02-19 23:15:36 +00:00
parent b45c82e5fa
commit 8522cf50b0
7 changed files with 153 additions and 11 deletions

View File

@ -1,4 +1,5 @@
using System.Web.Mvc; using System.Web.Mvc;
using Portoa.Persistence;
using Portoa.Web; using Portoa.Web;
using Portoa.Web.ErrorHandling; using Portoa.Web.ErrorHandling;
using VideoGameQuotes.Api; using VideoGameQuotes.Api;
@ -17,6 +18,66 @@ namespace VideoGameQuotes.Web.Controllers {
this.adminService = adminService; this.adminService = adminService;
} }
[HttpGet]
public ActionResult Create() {
var model = new CreateAdminModel();
ResetCreateAdminModel(model);
return View(model);
}
private void ResetCreateAdminModel(CreateAdminModel model) {
model.Users = adminService.GetAllUsers();
}
[HttpPost]
public ActionResult Create(CreateAdminModel model) {
if (!ModelState.IsValid) {
ResetCreateAdminModel(model);
return View(model);
}
try {
var user = new User {
Username = model.Username,
Group = UserGroup.Admin
};
if (model.UserId > 0) {
user = adminService.GetUser(model.UserId);
if (user.Group == UserGroup.Admin) {
ModelState.AddModelError("UserId", string.Format("The user {0} is already an admin", user.Username));
} else {
user.Group = UserGroup.Admin;
}
if (user.Username == null) {
if (string.IsNullOrWhiteSpace(model.Username)) {
ModelState.AddModelError("Username", "Username must be given if the user does not have a username");
} else {
user.Username = model.Username;
}
}
} else if (string.IsNullOrWhiteSpace(model.Username)) {
ModelState.AddModelError("Username", "Username must be non-empty if creating a new user");
}
if (!ModelState.IsValid) {
ResetCreateAdminModel(model);
return View(model);
}
user.IpAddress = null; //must delete ip address or it's kind of a glaring security hole
user.ChangePassword(model.Password);
adminService.SaveUser(user);
return View("CreateAdminSuccess", model);
} catch (EntityNotFoundException) {
ModelState.AddModelError("UserId", string.Format("User not found for id {0}", model.UserId));
ResetCreateAdminModel(model);
return View(model);
}
}
public ActionResult Index() { public ActionResult Index() {
return View(); return View();
} }

View File

@ -0,0 +1,28 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using VideoGameQuotes.Api;
namespace VideoGameQuotes.Web.Models {
public class CreateAdminModel {
[DisplayName("Use existing user")]
public int UserId { get; set; }
public string Username { get; set; }
[Required]
public string Password { get; set; }
public IEnumerable<User> Users { get; set; }
public IEnumerable<SelectListItem> GetUserList() {
return new[] { new SelectListItem { Text = "--none--", Value = "0" } }
.Concat(Users
.Where(user => user.Group != UserGroup.Admin)
.OrderBy(user => user.Username)
.ThenBy(user => user.IpAddress)
.Select(user => new SelectListItem { Text = user.Username ?? user.IpAddress, Value = user.Id.ToString() })
);
}
}
}

View File

@ -7,6 +7,8 @@ namespace VideoGameQuotes.Web.Services {
public interface IAdministrationService { public interface IAdministrationService {
User SaveUser(User user); User SaveUser(User user);
IEnumerable<Quote> GetFlaggedQuotes(); IEnumerable<Quote> GetFlaggedQuotes();
User GetUser(int id);
IEnumerable<User> GetAllUsers();
} }
public class AdministrationService : IAdministrationService { public class AdministrationService : IAdministrationService {
@ -31,5 +33,14 @@ namespace VideoGameQuotes.Web.Services {
.OrderByDescending(quote => quote.Flags.Count()); .OrderByDescending(quote => quote.Flags.Count());
} }
[UnitOfWork]
public User GetUser(int id) {
return userRepository.FindById(id);
}
[UnitOfWork]
public IEnumerable<User> GetAllUsers() {
return userRepository.Records;
}
} }
} }

View File

@ -91,6 +91,7 @@
<Compile Include="Models\BrowseModel.cs" /> <Compile Include="Models\BrowseModel.cs" />
<Compile Include="Models\BrowseModelBinder.cs" /> <Compile Include="Models\BrowseModelBinder.cs" />
<Compile Include="Models\ChangePasswordModel.cs" /> <Compile Include="Models\ChangePasswordModel.cs" />
<Compile Include="Models\CreateAdminModel.cs" />
<Compile Include="Models\MainMenuModel.cs" /> <Compile Include="Models\MainMenuModel.cs" />
<Compile Include="Models\QualifiedBrowseModel.cs" /> <Compile Include="Models\QualifiedBrowseModel.cs" />
<Compile Include="Models\PagedQuoteCollectionModel.cs" /> <Compile Include="Models\PagedQuoteCollectionModel.cs" />
@ -131,6 +132,8 @@
<Content Include="media\images\search.png" /> <Content Include="media\images\search.png" />
<Content Include="media\js\jquery.cookie.js" /> <Content Include="media\js\jquery.cookie.js" />
<Content Include="media\js\vgquotes.js" /> <Content Include="media\js\vgquotes.js" />
<Content Include="Views\Admin\Create.aspx" />
<Content Include="Views\Admin\CreateAdminSuccess.aspx" />
<Content Include="Views\Admin\FlaggedQuote.ascx" /> <Content Include="Views\Admin\FlaggedQuote.ascx" />
<Content Include="Views\Admin\Flags.aspx" /> <Content Include="Views\Admin\Flags.aspx" />
<Content Include="Views\Admin\Index.aspx" /> <Content Include="Views\Admin\Index.aspx" />

View File

@ -0,0 +1,30 @@
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<VideoGameQuotes.Web.Models.CreateAdminModel>" MasterPageFile="~/Views/Shared/Site.Master" %>
<%@ Import Namespace="Portoa.Web.Util" %>
<asp:Content runat="server" ID="Title" ContentPlaceHolderID="TitleContent">Create Admin</asp:Content>
<asp:Content runat="server" ID="Main" ContentPlaceHolderID="MainContent">
<h2>Create Admin</h2>
<%= Html.ValidationSummary("Some errors occurred") %>
<% using (Html.BeginForm()) { %>
<p>
<%= Html.LabelFor(model => model.UserId) %> <%= Html.ValidationMessageFor(model => model.UserId) %>
<br />
<%= Html.DropDownListFor(model => model.UserId, Model.GetUserList()) %>
</p>
<p>
<%= Html.LabelFor(model => model.Username) %> <%= Html.ValidationMessageFor(model => model.Username) %>
<br />
<%= Html.TextBoxFor(model => model.Username) %>
</p>
<p>
<%= Html.LabelFor(model => model.Password) %> <%= Html.ValidationMessageFor(model => model.Password) %>
<br />
<%= Html.PasswordFor(model => model.Password) %>
</p>
<%= Html.Submit("Create Admin") %>
<% } %>
</asp:Content>

View File

@ -0,0 +1,9 @@
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<VideoGameQuotes.Web.Models.CreateAdminModel>" MasterPageFile="~/Views/Shared/Site.Master" %>
<asp:Content runat="server" ID="Title" ContentPlaceHolderID="TitleContent">Create Admin</asp:Content>
<asp:Content runat="server" ID="Main" ContentPlaceHolderID="MainContent">
<h2>Success</h2>
<p>
The user <%= Html.ActionLink(Model.Username, "user", "admin", new { id = Model.UserId }, null) %> is now
an admin.
</p>
</asp:Content>

View File

@ -32,7 +32,7 @@ alter table quote_flag drop foreign key fk_flag_user
; ;
alter table quote_flag drop foreign key fk_flag_quote alter table quote_flag drop foreign key fk_quote_flag
; ;
@ -61,6 +61,8 @@ alter table quote_category_map drop foreign key FK5892F846C2AA09DD
drop table if exists publisher; drop table if exists publisher;
drop table if exists category;
drop table if exists vgquote_user; drop table if exists vgquote_user;
drop table if exists quote_flag; drop table if exists quote_flag;
@ -71,8 +73,6 @@ alter table quote_category_map drop foreign key FK5892F846C2AA09DD
drop table if exists system; drop table if exists system;
drop table if exists category;
create table vote ( create table vote (
vote_id INTEGER NOT NULL AUTO_INCREMENT, vote_id INTEGER NOT NULL AUTO_INCREMENT,
created DATETIME not null, created DATETIME not null,
@ -113,6 +113,13 @@ alter table quote_category_map drop foreign key FK5892F846C2AA09DD
primary key (publisher_id) primary key (publisher_id)
); );
create table category (
category_id INTEGER NOT NULL AUTO_INCREMENT,
category_name VARCHAR(255) not null unique,
created DATETIME not null,
primary key (category_id)
);
create table vgquote_user ( create table vgquote_user (
user_id INTEGER NOT NULL AUTO_INCREMENT, user_id INTEGER NOT NULL AUTO_INCREMENT,
username VARCHAR(50), username VARCHAR(50),
@ -159,13 +166,6 @@ alter table quote_category_map drop foreign key FK5892F846C2AA09DD
primary key (system_id) primary key (system_id)
); );
create table category (
category_id INTEGER NOT NULL AUTO_INCREMENT,
category_name VARCHAR(255) not null unique,
created DATETIME not null,
primary key (category_id)
);
alter table vote alter table vote
add index (voter_id), add index (voter_id),
add constraint fk_vote_user add constraint fk_vote_user
@ -216,7 +216,7 @@ alter table quote_category_map drop foreign key FK5892F846C2AA09DD
alter table quote_flag alter table quote_flag
add index (quote_id), add index (quote_id),
add constraint fk_flag_quote add constraint fk_quote_flag
foreign key (quote_id) foreign key (quote_id)
references game_quote (quote_id); references game_quote (quote_id);