Commit for development environment setup
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using Csla;
|
||||
|
||||
namespace ProjectTracker.Library.Admin
|
||||
{
|
||||
[Serializable()]
|
||||
public class Role : BusinessBase<Role>
|
||||
{
|
||||
#region Business Methods
|
||||
|
||||
private int _id;
|
||||
private bool _idSet;
|
||||
private string _name = String.Empty;
|
||||
private byte[] _timestamp = new byte[8];
|
||||
|
||||
[System.ComponentModel.DataObjectField(true, true)]
|
||||
public int Id
|
||||
{
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
||||
get
|
||||
{
|
||||
CanReadProperty(true);
|
||||
if (!_idSet)
|
||||
{
|
||||
// generate a default id value
|
||||
_idSet = true;
|
||||
Roles parent = (Roles)this.Parent;
|
||||
int max = 0;
|
||||
foreach (Role item in parent)
|
||||
{
|
||||
if (item.Id > max)
|
||||
max = item.Id;
|
||||
}
|
||||
_id = max + 1;
|
||||
}
|
||||
return _id;
|
||||
}
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
||||
set
|
||||
{
|
||||
CanWriteProperty(true);
|
||||
if (!_id.Equals(value))
|
||||
{
|
||||
_idSet = true;
|
||||
_id = value;
|
||||
PropertyHasChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
||||
get
|
||||
{
|
||||
CanReadProperty(true);
|
||||
return _name;
|
||||
}
|
||||
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
|
||||
set
|
||||
{
|
||||
CanWriteProperty(true);
|
||||
if (value == null) value = string.Empty;
|
||||
if (!_name.Equals(value))
|
||||
{
|
||||
_name = value;
|
||||
PropertyHasChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override object GetIdValue()
|
||||
{
|
||||
return _id;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Validation Rules
|
||||
|
||||
protected override void AddBusinessRules()
|
||||
{
|
||||
ValidationRules.AddRule(
|
||||
Csla.Validation.CommonRules.StringRequired, "Name");
|
||||
}
|
||||
|
||||
protected override void AddInstanceBusinessRules()
|
||||
{
|
||||
ValidationRules.AddInstanceRule(NoDuplicates, "Id");
|
||||
}
|
||||
|
||||
private bool NoDuplicates(object target, Csla.Validation.RuleArgs e)
|
||||
{
|
||||
Roles parent = (Roles)this.Parent;
|
||||
foreach (Role item in parent)
|
||||
if (item.Id == _id && !ReferenceEquals(item, this))
|
||||
{
|
||||
e.Description = "Role Id must be unique";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Authorization Rules
|
||||
|
||||
protected override void AddAuthorizationRules()
|
||||
{
|
||||
AuthorizationRules.AllowWrite(
|
||||
"Id", "Administrator");
|
||||
AuthorizationRules.AllowWrite(
|
||||
"Name", "Administrator");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Factory Methods
|
||||
|
||||
internal static Role NewRole()
|
||||
{
|
||||
return new Role();
|
||||
}
|
||||
|
||||
internal static Role
|
||||
GetRole(Csla.Data.SafeDataReader dr)
|
||||
{
|
||||
return new Role(dr);
|
||||
}
|
||||
|
||||
private Role()
|
||||
{
|
||||
MarkAsChild();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Data Access
|
||||
|
||||
private Role(Csla.Data.SafeDataReader dr)
|
||||
{
|
||||
MarkAsChild();
|
||||
_id = dr.GetInt32("id");
|
||||
_idSet = true;
|
||||
_name = dr.GetString("name");
|
||||
dr.GetBytes("lastChanged", 0, _timestamp, 0, 8);
|
||||
MarkOld();
|
||||
}
|
||||
|
||||
internal void Insert(SqlConnection cn)
|
||||
{
|
||||
// if we're not dirty then don't update the database
|
||||
if (!this.IsDirty) return;
|
||||
|
||||
using (SqlCommand cm = cn.CreateCommand())
|
||||
{
|
||||
cm.CommandText = "addRole";
|
||||
DoInsertUpdate(cm);
|
||||
}
|
||||
}
|
||||
|
||||
internal void Update(SqlConnection cn)
|
||||
{
|
||||
// if we're not dirty then don't update the database.
|
||||
if (!this.IsDirty) return;
|
||||
|
||||
using (SqlCommand cm = cn.CreateCommand())
|
||||
{
|
||||
cm.CommandText = "updateRole";
|
||||
cm.Parameters.AddWithValue("@lastChanged", _timestamp);
|
||||
DoInsertUpdate(cm);
|
||||
}
|
||||
}
|
||||
|
||||
void DoInsertUpdate(SqlCommand cm)
|
||||
{
|
||||
cm.CommandType = CommandType.StoredProcedure;
|
||||
cm.Parameters.AddWithValue("@id", _id);
|
||||
cm.Parameters.AddWithValue("@name", _name);
|
||||
SqlParameter param =
|
||||
new SqlParameter("@newLastChanged", SqlDbType.Timestamp);
|
||||
param.Direction = ParameterDirection.Output;
|
||||
cm.Parameters.Add(param);
|
||||
|
||||
cm.ExecuteNonQuery();
|
||||
|
||||
_timestamp = (byte[])cm.Parameters["@newLastChanged"].Value;
|
||||
|
||||
MarkOld();
|
||||
}
|
||||
|
||||
internal void DeleteSelf(SqlConnection cn)
|
||||
{
|
||||
// if we're not dirty then don't update the database
|
||||
if (!this.IsDirty) return;
|
||||
|
||||
// if we're new then don't update the database
|
||||
if (this.IsNew) return;
|
||||
|
||||
DeleteRole(cn, _id);
|
||||
MarkNew();
|
||||
}
|
||||
|
||||
internal static void DeleteRole(SqlConnection cn, int id)
|
||||
{
|
||||
using (SqlCommand cm = cn.CreateCommand())
|
||||
{
|
||||
cm.CommandType = CommandType.StoredProcedure;
|
||||
cm.CommandText = "deleteRole";
|
||||
cm.Parameters.AddWithValue("@id", id);
|
||||
cm.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using Csla;
|
||||
using Csla.Data;
|
||||
|
||||
namespace ProjectTracker.Library.Admin
|
||||
{
|
||||
/// <summary>
|
||||
/// Used to maintain the list of roles
|
||||
/// in the system.
|
||||
/// </summary>
|
||||
[Serializable()]
|
||||
public class Roles :
|
||||
BusinessListBase<Roles, Role>
|
||||
{
|
||||
#region Business Methods
|
||||
|
||||
/// <summary>
|
||||
/// Remove a role based on the role's
|
||||
/// id value.
|
||||
/// </summary>
|
||||
/// <param name="id">Id value of the role to remove.</param>
|
||||
public void Remove(int id)
|
||||
{
|
||||
foreach (Role item in this)
|
||||
{
|
||||
if (item.Id == id)
|
||||
{
|
||||
Remove(item);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a role based on its id value.
|
||||
/// </summary>
|
||||
/// <param name="id">Id valud of the role to return</param>
|
||||
public Role GetRoleById(int id)
|
||||
{
|
||||
foreach (Role item in this)
|
||||
if (item.Id == id)
|
||||
return item;
|
||||
return null;
|
||||
}
|
||||
|
||||
protected override object AddNewCore()
|
||||
{
|
||||
Role item = Role.NewRole();
|
||||
Add(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Authorization Rules
|
||||
|
||||
public static bool CanAddObject()
|
||||
{
|
||||
return Csla.ApplicationContext.User.IsInRole("Administrator");
|
||||
}
|
||||
|
||||
public static bool CanGetObject()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool CanDeleteObject()
|
||||
{
|
||||
return Csla.ApplicationContext.User.IsInRole("Administrator");
|
||||
}
|
||||
|
||||
public static bool CanEditObject()
|
||||
{
|
||||
return Csla.ApplicationContext.User.IsInRole("Administrator");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Factory Methods
|
||||
|
||||
public static Roles GetRoles()
|
||||
{
|
||||
return DataPortal.Fetch<Roles>(new Criteria());
|
||||
}
|
||||
|
||||
private Roles()
|
||||
{
|
||||
this.AllowNew = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Data Access
|
||||
|
||||
[Serializable()]
|
||||
private class Criteria
|
||||
{ /* no criteria */ }
|
||||
|
||||
public override Roles Save()
|
||||
{
|
||||
// see if save is allowed
|
||||
if (!CanEditObject())
|
||||
throw new System.Security.SecurityException(
|
||||
"User not authorized to save roles");
|
||||
|
||||
// do the save
|
||||
Roles result;
|
||||
result = base.Save();
|
||||
// this runs on the client and invalidates
|
||||
// the RoleList cache
|
||||
RoleList.InvalidateCache();
|
||||
return result;
|
||||
}
|
||||
|
||||
protected override void DataPortal_OnDataPortalInvokeComplete(
|
||||
DataPortalEventArgs e)
|
||||
{
|
||||
if (ApplicationContext.ExecutionLocation ==
|
||||
ApplicationContext.ExecutionLocations.Server)
|
||||
{
|
||||
// this runs on the server and invalidates
|
||||
// the RoleList cache
|
||||
RoleList.InvalidateCache();
|
||||
}
|
||||
}
|
||||
|
||||
private void DataPortal_Fetch(Criteria criteria)
|
||||
{
|
||||
RaiseListChangedEvents = false;
|
||||
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
|
||||
{
|
||||
cn.Open();
|
||||
using (SqlCommand cm = cn.CreateCommand())
|
||||
{
|
||||
cm.CommandType = CommandType.StoredProcedure;
|
||||
cm.CommandText = "getRoles";
|
||||
|
||||
using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))
|
||||
while (dr.Read())
|
||||
this.Add(Role.GetRole(dr));
|
||||
}
|
||||
}
|
||||
RaiseListChangedEvents = true;
|
||||
}
|
||||
|
||||
[Transactional(TransactionalTypes.TransactionScope)]
|
||||
protected override void DataPortal_Update()
|
||||
{
|
||||
this.RaiseListChangedEvents = false;
|
||||
using (SqlConnection cn = new SqlConnection(Database.PTrackerConnection))
|
||||
{
|
||||
cn.Open();
|
||||
foreach (Role item in DeletedList)
|
||||
{
|
||||
item.DeleteSelf(cn);
|
||||
}
|
||||
DeletedList.Clear();
|
||||
|
||||
foreach (Role item in this)
|
||||
{
|
||||
if (item.IsNew)
|
||||
item.Insert(cn);
|
||||
else
|
||||
item.Update(cn);
|
||||
}
|
||||
}
|
||||
this.RaiseListChangedEvents = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user