Skip to content

Commit

Permalink
Added entities for Security area. #26
Browse files Browse the repository at this point in the history
  • Loading branch information
gius committed Jun 1, 2015
1 parent a7c3125 commit bcc6c76
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
<Compile Include="Domain\Configuration\Entities\Project.cs" />
<Compile Include="Domain\Configuration\ProjectsRepository.cs" />
<Compile Include="Domain\EntityBase.cs" />
<Compile Include="Domain\Security\Entities\Position.cs" />
<Compile Include="Domain\Security\Entities\Role.cs" />
<Compile Include="Domain\Security\Entities\RolePositionLink.cs" />
<Compile Include="Domain\Security\Entities\User.cs" />
<Compile Include="Domain\Security\Entities\UserPositionLink.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\RealDateTimeProvider.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baud.Deployment.BusinessLogic.Domain.Security.Entities
{
public class Position : EntityBase
{
public short ID { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }

public List<UserPositionLink> UserLinks { get; set; }
public List<RolePositionLink> RoleLinks { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baud.Deployment.BusinessLogic.Domain.Security.Entities
{
public class Role : EntityBase
{
public short ID { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }

public List<RolePositionLink> PositionLinks { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baud.Deployment.BusinessLogic.Domain.Security.Entities
{
public class RolePositionLink : EntityBase
{
public short RoleID { get; set; }
public short PositionID { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baud.Deployment.BusinessLogic.Domain.Security.Entities
{
public class User : EntityBase
{
public short ID { get; set; }
public string Login { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string PasswordHash { get; set; }

public DateTime ActiveFrom { get; set; }
public DateTime? ActiveTo { get; set; }

public string Note { get; set; }

public bool IsSystemUser { get; set; }

public List<UserPositionLink> PositionLinks { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baud.Deployment.BusinessLogic.Domain.Security.Entities
{
public class UserPositionLink : EntityBase
{
public short UserID { get; set; }
public short PositionID { get; set; }
}
}
46 changes: 5 additions & 41 deletions src/Server/DeploymentFramework/Database/BusinessContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,18 @@
using Baud.Deployment.BusinessLogic.DataAccess;
using Baud.Deployment.BusinessLogic.DataAccess.Contracts;
using Baud.Deployment.BusinessLogic.Domain.Configuration.Entities;
using Baud.Deployment.BusinessLogic.Domain.Security.Entities;

namespace Baud.Deployment.Database
{
public class BusinessContext : DbContext, IBusinessContext
public class BusinessContext : ContextBase, IBusinessContext
{
public IDbSet<Project> Projects { get; set; }

public void AttachAsModified<TEntity>(TEntity entity) where TEntity : class
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var dbEntityEntry = this.Entry(entity);
if (dbEntityEntry.State == EntityState.Detached)
{
Set<TEntity>().Attach(entity);
}

// marking all properties as modified
dbEntityEntry.State = EntityState.Modified;
}

public void AttachAsModified<TEntity>(TEntity entity, params System.Linq.Expressions.Expression<Func<TEntity, object>>[] modifiedProperties) where TEntity : class
{
var dbEntityEntry = this.Entry(entity);
if (dbEntityEntry.State == EntityState.Detached)
{
Set<TEntity>().Attach(entity);
}

if (modifiedProperties.Length > 0)
{
for (int i = 0; i < modifiedProperties.Length; i++)
{
var prop = dbEntityEntry.Property(modifiedProperties[i]);
prop.IsModified = true;
}
}
else
{
// marking all properties as modified
dbEntityEntry.State = EntityState.Modified;
}
}

public void MarkReferenceModified<TEntity, TProperty>(TEntity entity, System.Linq.Expressions.Expression<Func<TEntity, TProperty>> navigationProperty)
where TEntity : class
where TProperty : class
{
var reference = Entry(entity).Reference(navigationProperty);
reference.CurrentValue = reference.CurrentValue;
modelBuilder.Entity<Project>()
.ToTable("Project", "Configuration");
}
}
}
55 changes: 55 additions & 0 deletions src/Server/DeploymentFramework/Database/ContextBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Baud.Deployment.Database
{
public class ContextBase : DbContext
{
public void AttachAsModified<TEntity>(TEntity entity) where TEntity : class
{
var dbEntityEntry = this.Entry(entity);
if (dbEntityEntry.State == EntityState.Detached)
{
Set<TEntity>().Attach(entity);
}

// marking all properties as modified
dbEntityEntry.State = EntityState.Modified;
}

public void AttachAsModified<TEntity>(TEntity entity, params System.Linq.Expressions.Expression<Func<TEntity, object>>[] modifiedProperties) where TEntity : class
{
var dbEntityEntry = this.Entry(entity);
if (dbEntityEntry.State == EntityState.Detached)
{
Set<TEntity>().Attach(entity);
}

if (modifiedProperties.Length > 0)
{
for (int i = 0; i < modifiedProperties.Length; i++)
{
var prop = dbEntityEntry.Property(modifiedProperties[i]);
prop.IsModified = true;
}
}
else
{
// marking all properties as modified
dbEntityEntry.State = EntityState.Modified;
}
}

public void MarkReferenceModified<TEntity, TProperty>(TEntity entity, System.Linq.Expressions.Expression<Func<TEntity, TProperty>> navigationProperty)
where TEntity : class
where TProperty : class
{
var reference = Entry(entity).Reference(navigationProperty);
reference.CurrentValue = reference.CurrentValue;
}
}
}
3 changes: 3 additions & 0 deletions src/Server/DeploymentFramework/Database/Database.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BusinessContext.cs" />
<Compile Include="ContextBase.cs" />
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SecurityContext.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Baud.Deployment.Database.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<Baud.Deployment.Database.BusinessContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
ContextKey = "Baud.Deployment.Database.BusinessContext";
}

protected override void Seed(Baud.Deployment.Database.BusinessContext 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" }
// );
//
}
}
}
40 changes: 40 additions & 0 deletions src/Server/DeploymentFramework/Database/SecurityContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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.Database
{
public class SecurityContext : ContextBase
{
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Role>()
.ToTable("Role", "Security");
modelBuilder.Entity<Role>()
.HasMany(r => r.PositionLinks);

modelBuilder.Entity<RolePositionLink>()
.ToTable("Role_Position", "Security")
.HasKey(x => new { x.RoleID, x.PositionID });

modelBuilder.Entity<Position>()
.ToTable("Position", "Security");
modelBuilder.Entity<Position>()
.HasMany(p => p.RoleLinks);
modelBuilder.Entity<Position>()
.HasMany(p => p.UserLinks);

modelBuilder.Entity<UserPositionLink>()
.ToTable("User_Position", "Security")
.HasKey(x => new { x.UserID, x.PositionID });

modelBuilder.Entity<User>()
.ToTable("User", "Security");
modelBuilder.Entity<User>()
.HasMany(u => u.PositionLinks);
}
}
}

0 comments on commit bcc6c76

Please sign in to comment.