Added container accessor to Castle.Core
[castle.git] / Samples / Castle / PetStore.Model / User.cs
blob1c598b2882badc094dbfed83bcc8ae70404fb44f
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace PetStore.Model
17 using System;
18 using System.Security.Principal;
20 using Castle.ActiveRecord;
22 using NHibernate.Expression;
25 [ActiveRecord("`User`", DiscriminatorColumn="type", DiscriminatorType="String", DiscriminatorValue="user")]
26 public class User : ActiveRecordBase, IPrincipal
28 private int id;
29 private string login;
30 private string name;
31 private string email;
32 private string password;
33 private string userType;
35 [PrimaryKey]
36 public int Id
38 get { return id; }
39 set { id = value; }
42 [Property]
43 public string Login
45 get { return login; }
46 set { login = value; }
49 [Property]
50 public string Name
52 get { return name; }
53 set { name = value; }
56 [Property]
57 public string Email
59 get { return email; }
60 set { email = value; }
63 [Property]
64 public string Password
66 get { return password; }
67 set { password = value; }
70 /// <summary>
71 /// Exposing the discriminator column
72 /// has its restrictions
73 /// </summary>
74 [Property("type", Insert=false, Update=false)]
75 public string UserType
77 get { return userType; }
78 set { userType = value; }
81 public bool IsInRole(string role)
83 // We do not implement this functionality
84 return false;
87 public IIdentity Identity
89 get { return new GenericIdentity(name, "castle.authentication"); }
92 public static User Find(int id)
94 return (User) FindByPrimaryKey( typeof(User), id );
97 public static User FindByLogin(String login)
99 User[] users = (User[])
100 FindAll( typeof(User), Expression.Eq("Login", login) );
102 if (users.Length == 1)
104 return users[0];
107 return null;