I'm trying to create a scheme to set permissions on my system and these permissions are given to the user profile, where you can choose what each profile is allowed to do in each module. For this I have created some tables and I am in doubt how to do the mapping between them, mainly in the part of permissions.
In Java I do the mappings in the entities themselves and in NHibernate I am new so I still do not quite understand the concept of how to do the mappings. My question is between relating the Profile / Module and Permission entity. In case I created an entity only for Permission where it does the Profile relationship (HasOne) and Modulo (HasMany), is this correct?
Note: When defining the permissions for the profile, I want to display all the modules in a DataGridView with the options checkbox checkbox (insert, change, search, print) and thus define the permissions of each module in the permissoes
table %.
What is the best way to do this?
Entities
Profile
public class Perfil {
public virtual int id { set; get; }
public virtual string descricao { set; get; }
public Perfil() {
}
}
Modules
public class Modulo {
public virtual int id { set; get; }
public virtual string nome { set; get; }
public virtual string descricao { set; get; }
public Modulo() {
}
public Modulo(string nome, string descricao) {
this.nome = nome;
this.descricao = descricao;
}
}
Permissions
public class Permissao {
public virtual int id { set; get; }
public virtual Perfil perfil { set; get; }
public virtual IList<Modulo> modulos { set; get; }
public virtual int inserir { set; get; }
public virtual int alterar { set; get; }
public virtual int pesquisar { set; get; }
public virtual int imprimir { set; get; }
public Permissao() {
modulos = new List<Modulo>();
}
}
Mappings
Profile
public class PerfilMap : ClassMap<Perfil>{
public PerfilMap() {
Table("perfis");
Id(p => p.id).GeneratedBy.Identity();
Map(p => p.descricao).Unique().Not.Nullable();
}
}
Module
public class ModuloMap : ClassMap<Modulo>{
public ModuloMap() {
Table("modulos");
Id(m => m.id).GeneratedBy.Identity();
Map(m => m.nome).Unique().Not.Nullable();
Map(m => m.descricao).Not.Nullable();
}
}
Permission
public class PermissaoMap : ClassMap<Permissao>{
public PermissaoMap() {
Table("permissoes");
Id(p => p.id).GeneratedBy.Identity();
Map(p => p.inserir).CustomType<int>();
Map(p => p.alterar).CustomType<int>();
Map(p => p.pesquisar).CustomType<int>();
Map(p => p.imprimir).CustomType<int>();
HasOne(p => p.perfil);
HasMany(p => p.modulos).LazyLoad();
}
}
model