Can an attribute be used for several categories in ALFA? - authorization

A doctor can belong to subjectCat (the user that is trying to gain access) or to resourceCat (the referring physician of a medical examination the subject is trying to access).
As it appears to me, to support both cases I need to define the doctor for each category individually:
namespace subject {
namespace doctor {
attribute id {
category = subjectCat
id = "id"
type = string
}
attribute lastname {
category = subjectCat
id = "lastname"
type = string
}
//and 20 more attributes...
}
}
namespace resource {
namespace doctor {
attribute id {
category = resourceCat //this line is the only difference
id = "id"
type = string
}
attribute lastname {
category = resourceCat //this line is the only difference
id = "lastname"
type = string
}
//and 20 more attributes...
}
}
That's pretty cumbersome and bears a lot of redundancy. Is there anything I can do to avoid that?

You are right. You would redefine the attributes. In a way you are using the same object from your information model (e.g. doctor) but in one case that object (Doctor) acts as a subject. In another, it acts as an object you are protecting e.g.
A doctor can view a medical record of a patient they are assigned to.
An HR staff can view the salary of a doctor.
Yes, it means you would have to define the attribute itself in multiple categories. You can still leverage the namespace structure insofar as the combination of namespace and name remains unique.
You could do acme.user.staff.doctor and attribute name. You could then do acme.object.doctor and attribute name.
Note that Eclipse will let you do autocomplete too:

Related

NHibernate 4 child collection saved, but not re-loaded

I've got an NHibernate 4 project with several collection relationships. I'm unit-testing the object model, exercising all the collections. Most work fine, but in one case, the child collection is cascade-saved properly, but on loading the parent entity and examining the collection property, the child collection is empty.
Here are the abbreviated classes. GatewayUser is the parent object, and it has a collection of Student. The collection has a private backing property, and AddStudent/RemoveStudent methods.
Further complications: I'm using the NHibernate.AspNet.Identity library for OAuth2 user management, and GatewayUser inherits from IdentityUser. That in turn inherits from the the library's internal base entity class, which is different from my project's own base class.
public class GatewayUser : IdentityUser
{
public GatewayUser()
{
}
public virtual string FirstName { get; set; }
// ...More value properties and OAuth stuff omitted
// students associated with this user
private IList<Student> _students = new List<Student>();
public virtual IList<Student> Students
{
get { return new ReadOnlyCollection<Student>(_students); }
}
public virtual GatewayUser AddStudent(Student s)
{
if (_students.Contains(s))
return this;
s.GatewayUser = this;
_students.Add(s);
return this;
}
public virtual GatewayUser RemoveStudent(Student s)
{
if (_students.Contains(s))
{
_students.Remove(s);
}
return this;
}
Student is more ordinary; it inherits from my own BaseEntity class, has many value properties, and its own child collection of ProgramApplication items. Interestingly, this collection saves and loads fine; it's got the same structure (private backer, etc.) as the failing collection in GatewayUser.
The mapping is complicated, because the library internally maps its classes with NHiberante.Mapping.ByCode.Conformist classes (which I have no prior experience with).
I'm mapping my own classes with NHibernate automapping, because I have so many classes and properties to map. To get it all working, I copied the library's mapping helper class, and modified it a bit to add my base entity classes to it's list called baseEntityToIgnore. I also had to create a conformist mapping for GatewayUser, since it has a different base entity type, and my automapping wouldn't pick it up.
The unit test looks like this:
[Test]
public void GatewayUserCascadesStudents()
{
var u = new GatewayUser() { FirstName = "Mama", LastName = "Bear", UserName = "somebody#example.com" };
var s1 = new Student() { FirstName = "First", LastName = "Student" };
var s2 = new Student() { FirstName = "Second", LastName = "Student" };
u.AddStudent(s1).AddStudent(s2);
using (var s = NewSession())
using (var tx = s.BeginTransaction())
{
s.Save(u);
tx.Commit();
}
GatewayUser fetched = null;
int count = 0;
using (var s = NewSession())
{
fetched = s.Get<GatewayUser>(u.Id);
count = fetched.Students.Count;
}
Assert.AreEqual(2, count);
}
The generated SQL inserts into both AspNetUsers and GatewayUser (reflecting the inheritance relationship), and inserts two records into Student. All good. On fetching, the SELECT joins the two user tables, and I get a GatewayUser object, but accessing the Students collection does not trigger a SELECT on the Student table. But if I change the mapping to Lazy(CollectionLazy.NoLazy), the SQL to select eagerly load Students appears in the log, but the collection is not populated. If I switch the database from SQLite to Sql Server, I see the student records in the table. The generated SQL (when NoLazy is applied) will fetch them. So on the database end, things look fine.
I have to think my Frankenstein mapping situation is to blame. I'm mixing the library's conformist mapping with Fluent mapping, and there are two different base entity classes. However, the generated schema looks correct, and the save cascades correctly, so I don't know if that's the issue.
Found my own answer. My mapping of the parent class's list was like this:
public class GatewayUserMap : JoinedSubclassMapping
{
public GatewayUserMap()
{
Key(g => g.Column("Id"));
Property(c => c.FirstName, m => m.Length(50));
// ... more properties
List(gu => gu.Students, map =>
{
map.Key(c => c.Column("GatewayUser_Id"));
map.Cascade(Cascade.All | Cascade.DeleteOrphans);
map.Index(li => li.Column("ListIndex"));
map.Access(Accessor.Field | Accessor.NoSetter);
}
);
}
}
I have a private backing field for the collection. Removing Accessor.NoSetter from the collection mapping fixed it. In fact, it still worked without Accessor.Field -- I guess the mapper does a good job of looking around for one, and using it if found. Changing the name of the private backer from "_students" to "funnyName" prevented the mapper from finding it.

Sql error when using GORM's dynamic FindAllBy method - #2 is not set

I have those domain classes:
class Person{
String username
}
class Course {
String code
static hasMany = [events:Event]
}
class Event {
Long id
String name
(...)
static belongsTo = [course:Course, parallel:Parallel]
static hasMany = [teachers: Person, students: Person,bought: Exchange, sold: Exchange]
}
And what I want to do is find all the events associated with a student and a course. I did this query:
(...)
println "User: ${person.username}"
println "Course: ${course.code}"
Set<Person> persons = []
persons.add(person)
def events = Event.findAllByCourseAndStudents(course, persons)
}
But that is giving me an error ERROR util.JDBCExceptionReporter - Parameter "#2" is not set; SQL statement:
What am I missing here?
The issue with the second parameter in your query Students is the fact it's an 1:M (one to many) association and querying these associations using dynamic finders is not supported.
Take a look at this quick overview of the various types of querying supported by GORM/Grails to get a better understanding of what kind of options are available to you and when to use them.
As you can see using a criteria is really what you need in this case:
def events = Event.createCriteria().list() {
eq('course', course)
students {
eqId(person.id)
}
}
}
The above only matches against a single person, but you can modify this further to use a list of persons as you intend. I will leave that exercise to you so you can become familiar with how to create and use criteria.

NHibernate ExcludeProperty: Why is there no IncludeProperty

The Example class is great if you are specifying which properties you want to exclude from the example. But what if you want to specify which properties to include?
Take this example: looking for people in the database that have the same name.
A Person object has many properties. So to use the NHibernate.Criterion.Example object I would have to specify every field to exclude - which could be many.
Why is there no IncludeProperty method?
I have a Person object and I want to see if it is a duplicte based on pre-set business rules (FirstName, LastName, DateOfBirth). These rules could be changed to include a postcode or something else - and I'd like to make that configurable.
Is there an easy way around this?
I have a solution to the IncludeProperty issue:
private Type persitentType = typeof(T);
public IList<T> GetByExample(T exampleInstance, params string[] propertiesToInclude)
{
// get the properties that will be excluded
List<string> propertiesToExclude =
persitentType.GetProperties().Where(p => propertiesToInclude.Contains(p.Name) == false).Select(p => p.Name).ToList();
// create the criteria based on the example and excluding the given properties
ICriteria criteria = NHibernateSession.CreateCriteria(persitentType);
Example example = Example.Create(exampleInstance);
foreach (string propertyToExclude in propertiesToExclude)
{
example.ExcludeProperty(propertyToExclude);
}
criteria.Add(example);
// return the result
return criteria.List<T>();
}
Add this method to your repository class. It uses reflection to determine what properties the specified object has, and then finds the properties to exclude based on those that have been specified as includes.

ASP.NET MVC 2 RC model binding with NHibernate and dropdown lists

I have problem with model binding in my ASP.NET MVC 2 RC application that uses NHibernate for data access. We are trying to build the application in a Ruby on Rails way and have a very simple architecture where the domain entities are used all the way from the database to the view.
The application has a couple of domain entities which can be illustrated by the following two classes:
public class Product {
...
public Category Category { get; set; }
}
public class Category {
public int Id { get; set; }
public string Name { get; set; }
}
In the view that renders the edit form has the following statement to display a dropdown list:
<%= Html.DropDownListFor(model => model.Category.Id,
new SelectList(ViewData["categories"] as IList<Category>, "Id", "Name"),
"-- Select Category --" ) %>
Please disregard the use of "non-typed" view data to hold the category collection.
The action method that receives the form post is similar to to the following. Note that the TransactionFilter attribute adds NHibernate transaction handling and commits the transaction if no exceptions occur and validation succeeds.
[HttpPost]
[TransactionFilter]
public ActionResult Edit(int id, FormCollection collection) {
var product = _repository.Load(id);
// Update the product except the Id
UpdateModel(product, null, null, new[] {"Id"}, collection);
if (ModelState.IsValid) {
return RedirectToAction("Details", new {id});
}
return View(product);
}
My issue is that the product.Category.Id is set with the value selected in the form, e.g. Category.Id = "2". Using the default model binder results in the following type of NHibernate exception:
identifier of an instance of Name.Space.Entities.Category was altered from 4 to 2
That makes a lot of sense since the product already has a category assigned and only the primary key of that existing category is being changed. Another category instance should have been assigned instead.
I guess a custom ModelBinder can be created to handle this issue but is there a simpler way to make this work? Can (and should) the domain entities be modified to handle this?
I solved a similar problem with combo boxes on my edit page by changing the following line
#Html.DropDownListFor(x => x.Employee.Id, new SelectList(ViewBag.Employees, "Id", "DisplayName"))
by
#Html.DropDownListFor(x => x.Employee, new SelectList(ViewBag.Employees, "Id", "DisplayName"))
So I removed the '.Id' like Bryan suggested. Before the change, the model only contained the Id of the Employee and after the change, the Binder also loaded all the details of the employee into the model.
I've used similar techniques with Linq to SQL classes before with no problems. I don't think you'd need a custom ModelBinder for this. UpdateModel should be updating the Product class you are passing into it - not the Category sub-class attached to it. Check the html generated by the DropDownListFor helper. What is the name of the element? It should be the name of the foreign-key field in your Products table (e.g. "CategoryID" or "Product.CategoryID" not "Category.Id"). If it's "Category.Id" - try changing the first parameter of the DropDownListFor to either "model => model.Category" or "model => model.CategoryID" (or whatever the foreign key field is). This should cause UpdateModel to only update the foreign-key field in the Product class and not the Category class ID.
The solution we chose at the time was something similar to this:
TryUpdateModel(product, null, null, new[] {"Category"}, collection);
int categoryId;
if (int.TryParse(collection["Category.Id"], NumberStyles.Integer, CultureInfo.InvariantCulture, out categoryId) && categoryId > 0) {
product.Category = _categoryRepository.Load(categoryId);
}
else {
product.Category = null;
}
We simply tell the model binder to exclude the association property and handle that manually. Not pretty but worked at the time....

NHibernate.Mapping.Attributes.Filter

I'm mapping my database tables using NHibernate with NHibernate.Mapping.Attributes library and I got stuck to get the Filter attributes to work.
Suppose a class A that has a set of objects of class B. So, I have, the following:
[NHibernate.Mapping.Attributes.Set(0, Inverse = true, Lazy = NHibernate.Mapping.Attributes.CollectionLazy.False)]
[NHibernate.Mapping.Attributes.Key(1, Column = "ClassAId")]
[NHibernate.Mapping.Attributes.OneToMany(2, Class = "ClassB, Assembly")]
public virtual ISet<ClassB> ClassBs { get; set; }
I want to create a filter on this collection to bring only class B objects that satisfy a given criteria, such as Status = 1.
How can I create such Filter?
The where parameter of the Set mapping should be able help you out. Per the documentation the where parameter:
where: (optional) specify an arbitrary
SQL WHERE condition to be used when
retrieving or removing the collection
(useful if the collection should
contain only a subset of the available
data)
So to filter on Status (assuming Status is a SQL column in the table mapped for ClassB - though this column does not have to be mapped in the NHibernate mapping).
[NHibernate.Mapping.Attributes.Set(0,...., Where = "Status = 1", .....)]
...
public virtual ISet<ClassB> ClassBs { get; set; }