1
I am trying to create a scheme to define permissions on my system and these permissions are given to the user profile, where one can choose what each profile is authorized to do on each module. For this I created some tables and I am in doubt of how to map between them, mainly in the permissions part.
In Java I do the mapping on the entities themselves and in Nhibernate I’m new so I still don’t understand the concept of how to do the mapping. My question is between relating the entity Profile/Module and Permission. In case I created an entity only for Permissao where it makes the relationship of Profile(Hasone) and Module(Hasmany), this is correct ?
Note: When setting the permissions for the profile, I want to display all the modules in a Datagridview with checkbox checkbox options (insert, change, search, print) and so set the permissions of each module in the table permissoes
.
What’s the best way to do it ?
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
I don’t understand! What’s your question? Is something going wrong? Is your question about modeling? Explain it better!
– Fernando Leal
@Fernando My question is whether my mapping is right according to my model. See that table
permissoes
eh composed in it I get the id of theperfil
and the ids ofmodulo
and tbm set the permissions by module. The way I did the mapping is right ?– FernandoPaiva