Skip to content

Commit

Permalink
Added simple UI for Users management. #27
Browse files Browse the repository at this point in the history
  • Loading branch information
gius committed Jun 8, 2015
1 parent 379fa53 commit b22d5c5
Show file tree
Hide file tree
Showing 44 changed files with 1,261 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@
<Compile Include="Domain\Security\Entities\RolePositionLink.cs" />
<Compile Include="Domain\Security\Entities\User.cs" />
<Compile Include="Domain\Security\Entities\UserPositionLink.cs" />
<Compile Include="Domain\Security\Queries\UserQueries.cs" />
<Compile Include="QueryableExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\RealDateTimeProvider.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ namespace Baud.Deployment.BusinessLogic.Domain.Security.Contracts
public interface IUsersRepository
{
IQueryable<User> GetUsers();

User GetUserDetail(short id);

void UpdateUser(short id, User user);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -18,6 +19,7 @@ public class User : EntityBase
public DateTime ActiveFrom { get; set; }
public DateTime? ActiveTo { get; set; }

[DataType(DataType.MultilineText)]
public string Note { get; set; }

public bool IsSystemUser { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Baud.Deployment.BusinessLogic.Domain.Security.Entities;

namespace Baud.Deployment.BusinessLogic.Domain.Security.Queries
{
public static class UserQueries
{
public static IQueryable<User> FilterByID(this IQueryable<User> query, int userID)
{
return query.Where(x => x.ID == userID);
}

public static IQueryable<User> OnlyNonSystem(this IQueryable<User> query)
{
return query.Where(x => !x.IsSystemUser);
}

public static IQueryable<User> OnlyActive(this IQueryable<User> query, DateTime? date = null)
{
DateTime chosenDate = date ?? DateTime.Now;
return query.Where(x => x.ActiveFrom <= chosenDate && (x.ActiveTo == null || x.ActiveTo > chosenDate));
}

public static IQueryable<User> OnlyInactive(this IQueryable<User> query, DateTime? date = null)
{
DateTime chosenDate = date ?? DateTime.Now;
return query.Where(x => x.ActiveFrom > chosenDate || x.ActiveTo < chosenDate);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Baud.Deployment
{
public static class QueryableExtensions
{
public static IQueryable<T> Filter<T>(this IQueryable<T> query, string filterValue, Expression<Func<T, bool>> predicate)
{
if (!string.IsNullOrEmpty(filterValue))
{
return query.Where(predicate);
}
else
{
return query;
}
}

public static IQueryable<TItem> Filter<TItem, TStruct>(this IQueryable<TItem> query, IEnumerable<TStruct> filterValue, Expression<Func<TItem, bool>> predicate) where TStruct : struct
{
if (filterValue != null && filterValue.Any())
{
return query.Where(predicate);
}
else
{
return query;
}
}

public static IQueryable<TItem> Filter<TItem, TStruct>(this IQueryable<TItem> query, TStruct? filterValue, Expression<Func<TItem, bool>> predicate) where TStruct : struct
{
if (filterValue != null)
{
return query.Where(predicate);
}
else
{
return query;
}
}
}
}
2 changes: 1 addition & 1 deletion src/Server/DeploymentFramework/Database/Database.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<Compile Include="Contracts\IDbContext.cs" />
<Compile Include="Contracts\IDbContextProvider.cs" />
<Compile Include="Contracts\IRepositoryProvider.cs" />
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Migrations\SecurityConfiguration.cs" />
<Compile Include="NonTrackingDbContextProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RepositoryBase.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Baud.Deployment.Database.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using Baud.Deployment.BusinessLogic.Domain.Security.Entities;

internal sealed class SecurityConfiguration : DbMigrationsConfiguration<Baud.Deployment.Database.Security.SecurityDbContext>
{
public SecurityConfiguration()
{
AutomaticMigrationsEnabled = true;
}

protected override void Seed(Baud.Deployment.Database.Security.SecurityDbContext context)
{
var now = DateTime.Now;

context.Users.AddOrUpdate(
u => u.Login,
new User { Login = "test", FirstName = "John", LastName = "Tester", ActiveFrom = new DateTime(2015, 5, 31), Email = "[email protected]", Created = now },
new User { Login = "demo", FirstName = "Jack", LastName = "Presenter", ActiveFrom = new DateTime(2015, 5, 31), Email = "[email protected]", Created = now });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Baud.Deployment.BusinessLogic.Domain.Security.Contracts;
using Baud.Deployment.BusinessLogic.Domain.Security.Entities;
using Baud.Deployment.BusinessLogic.Domain.Security.Queries;

namespace Baud.Deployment.Database.Security
{
Expand All @@ -17,7 +18,25 @@ public UsersRepository(SecurityDbContext context)

public IQueryable<User> GetUsers()
{
return Context.Users;
return Context.Users.OnlyNonSystem();
}

public User GetUserDetail(short id)
{
return Context.Users.FilterByID(id).FirstOrDefault();
}

public void UpdateUser(short id, User user)
{
user.ID = id;

Context.AttachAsModified(user,
x => x.FirstName,
x => x.LastName,
x => x.Email,
x => x.Note,
x => x.ActiveFrom,
x => x.ActiveTo);
}
}
}
19 changes: 0 additions & 19 deletions src/Server/DeploymentFramework/Web/App_Start/ContainerConfig.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Web.Mvc;
using ChameleonForms;
using ChameleonForms.ModelBinders;

[assembly: WebActivator.PreApplicationStartMethod(typeof(Baud.Deployment.Web.App_Start.RegisterChameleonFormsComponents), "Start")]

namespace Baud.Deployment.Web.App_Start
{
public static class RegisterChameleonFormsComponents
Expand All @@ -12,6 +13,8 @@ public static void Start()
{
System.Web.Mvc.ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder());
System.Web.Mvc.ModelBinders.Binders.Add(typeof(DateTime?), new DateTimeModelBinder());

FormTemplate.Default = new ChameleonForms.Templates.TwitterBootstrap3.TwitterBootstrapFormTemplate();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Web;
using System.Web.Mvc;
using Baud.Deployment.BusinessLogic.Domain.Security.Contracts;
using Baud.Deployment.Web.Areas.Security.Models.Users;
using Baud.Deployment.Web.Framework.Security;
using Baud.Deployment.Web.Framework.Web;

namespace Baud.Deployment.Web.Areas.Security.Controllers
{
Expand All @@ -18,14 +20,76 @@ public UsersController(Func<ISecurityUow> securityUow)
}

////[RequirePermission(Permissions.Security.UserRead)]
public virtual ActionResult Index()
public virtual ActionResult Index(IndexFilter filter, PagingData paging)
{
paging.PageSize = 2; // only for testing

ViewBag.Filter = filter;

using (var uow = _securityUow())
{
var usersQuery = uow.Users.GetUsers();
usersQuery = filter.Apply(usersQuery);
var data = usersQuery.ToPagedList(paging);

return View(data);
}
}

public virtual ActionResult Detail(short id)
{
using (var uow = _securityUow())
{
var user = uow.Users.GetUserDetail(id);

if (user == null)
{
return HttpNotFound();
}

return View(user);
}
}

public virtual ActionResult Edit(short id)
{
using (var uow = _securityUow())
{
var users = uow.Users.GetUsers().ToList();
var user = uow.Users.GetUserDetail(id);

if (user == null)
{
return HttpNotFound();
}

return View(user);
}
}

[HttpPost]
[ValidateAntiForgeryToken]
public virtual ActionResult Edit(short id, FormCollection form)
{
using (var uow = _securityUow())
{
var user = uow.Users.GetUserDetail(id);

if (user == null)
{
return HttpNotFound();
}

return View();
if (!TryUpdateModel(user))
{
return View(user);
}

uow.Users.UpdateUser(id, user);
uow.Commit();

// TODO add confirmation toast message
return RedirectToAction(Actions.Detail(id));
}
}
}
}
Loading

0 comments on commit b22d5c5

Please sign in to comment.