- VAL.src.zip
- VAL
- Database
- packages
- EntityFramework.4.2.0.0
- lib
- net40
- EntityFramework.dll
- log4net.1.2.10
- lib
- 1.0
- log4net.dll
- 1.1
- log4net.dll
- 2.0
- log4net.dll
- log4net.1.2.11
- lib
- net10-full
- log4net.dll
- net11-full
- log4net.dll
- net20-cf
- log4net.dll
- net20-full
- log4net.dll
- net35-client
- log4net.dll
- net35-full
- log4net.dll
- net40-client
- log4net.dll
- net40-full
- log4net.dll
- Moq.4.0.10827
- lib
- NET35
- Moq.dll
- NET40
- Moq.dll
- Silverlight4
- Castle.Core.dll
- Moq.Silverlight.dll
- License.txt
- Moq.chm
- repositories.config
- structuremap.2.6.3
- lib
- StructureMap.dll
- READ-ME-First!.txt
- VAL.BusinessService
- VAL.ClientService
- VAL.Common
- VAL.Contracts
- VAL.Controls
- VAL.Data
- VAL.GUI
- VAL.Model
- VAL.Service
- VAL.sln
- VAL.Tests
- VAL.User.Documentation
- VAL.zip
- VisualApplicationLauncher
- Database
- Local.testsettings
- NRepo
- ProjectReferences
- EFCachingProvider.dll
- EFCachingProvider.Web.dll
- EFProviderWrapperToolkit.dll
- EFTracingProvider.dll
- log4net.dll
- StructureMap.dll
- READ-ME-First!.txt
- TraceAndTestImpact.testsettings
- VAL.BusinessService
- VAL.Common
- VAL.Contracts
- VAL.Controls
- VAL.Data
- VAL.GUI
- VAL.Model
- VAL.Service
- VAL.sln
- VAL.Tests
- VAL.User.Documentation
- VAL.vsmdi
|
namespace VAL.BusinessService
{
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.ServiceModel;
using VAL.Common;
using VAL.Common.Properties;
using VAL.Contracts;
using VAL.Model;
using VAL.Repository;
/// <summary>
/// The user service allows the client application access to user and file information
/// required to use the main window of the application
/// </summary>
public class UserDomainService : DomainServiceBase, IUserService
{
/// <summary>
/// Ctor
/// </summary>
public UserDomainService(IGroupService groupService,
IUserRepository repository)
{
this.Repository = repository;
}
/// <summary>
/// Access to the repository
/// </summary>
private IUserRepository Repository { get; set; }
private IGroupService GroupService { get; set; }
#region IUserService Members
/// <summary>
/// Gets a user object based on the windows identity name
/// </summary>
/// <param name="userName">The users windows identity name</param>
/// <returns>A <see cref="User"/> object</returns>
/// <exception cref="UserInactiveException">The user is currently set to inactive</exception>
/// <exception cref="UserNotFoundException">The specified user name was not found in the database</exception>
public User GetUser(string userName)
{
#region Enter method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
#region Guard Parameter Validation
Guard.Against<ArgumentException>(string.IsNullOrEmpty(userName), "You must specify a user name");
#endregion
var query =
from u in Repository.Query
where u.WindowsIdentityName.ToUpper() == userName.ToUpper()
select u;
var user = query.FirstOrDefault();
#region Exit Method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
return user;
}
public User GetUser(int id)
{
#region Enter method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
#region Guard Parameter Validation
Guard.Against<ArgumentException>(id <= 0, "Invalid user id specified");
#endregion
var query =
from u in Repository.Query
where u.Id == id
select u;
var user = query.FirstOrDefault();
#region Exit Method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
return user;
}
/// <summary>
/// Gets a list of all active <see cref="User"/>s that are defined within a particular <see cref="Group"/>
/// </summary>
/// <param name="groupId">The unique ID of the group</param>
/// <returns>List of users defined within a group</returns>
public User[] GetGroupUsers(int groupId)
{
#region Enter method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
#region Guard Parameter Validation
Guard.Against<ArgumentException>(groupId <= 0, "You must specify a group id");
#endregion
var users = Repository.Query.Where(
u => u.GroupUsers.Any
(gu => gu.GroupID == groupId && u.IsActive == true))
.ToArray();
#region Exit Method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
return users;
}
#endregion
/// <summary>
/// Gets a list of all <see cref="User"/>s that are defined in the database
/// </summary>
/// <returns>List of Users</returns>
public User[] GetUsers()
{
#region Enter method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
var users = Repository.All();
#region Exit Method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
return users;
}
/// <summary>
/// Gets a list of all active <see cref="User"/>s that are defined in the database
/// </summary>
/// <returns>List of active Users</returns>
public User[] GetActiveUsers()
{
#region Enter method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
var query =
from u in Repository.Query
where u.IsActive == true
select u;
var users = query.ToArray();
#region Exit Method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
return users;
}
/// <summary>
/// Either inserts or updates a <see cref="User"/> instance
/// </summary>
/// <param name="file">The <see cref="User"/> to persist to the database</param>
public User SaveUser(User user)
{
#region Enter method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
#region Guard Parameter Validation
Guard.Against<ArgumentNullException>(user == null, "You must specify a valid user object");
#endregion
#region Business Rules Enforcement
if (!user.IsValid)
throw new VAL.Common.BusinessRuleException(user.ErrorMessage);
#endregion
if (user.Id == 0)
{
// If this is a new user, then check for an existing user with the same
// windows identity name and throw an exception if one exists
var existingUser = GetUser(user.WindowsIdentityName);
if (existingUser != null)
{
throw new VAL.Common.UserAlreadyExistsException(string.Format("User {0} already exists in the database!", user.WindowsIdentityName));
}
}
user = Repository.Add(user, user.Id == 0);
#region Exit Method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
return user;
}
/// <summary>
/// Deletes a user from the database that matches the specified ID
/// </summary>
/// <param name="userId">The unique ID of the user</param>
public void DeleteUser(int userId)
{
#region Enter method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
#region Guard Parameter Validation
Guard.Against<ArgumentException>(userId <= 0, "You must specify a valid user id");
#endregion
Repository.Delete(u => u.Id == userId);
#region Exit Method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
}
/// <summary>
/// Clones a set of permissions from one user to another
/// </summary>
/// <param name="userId">The user ID to use when cloning permissions</param>
/// <param name="targetUserId">The unique ID of the user to apply the permissions to</param>
public void CloneUserPermissions(User user, int targetUserId)
{
#region Enter method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Entered " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
#region Guard Parameter Validation
Guard.Against<ArgumentNullException>(user == null, "user object cannot be null");
Guard.Against<ArgumentException>(targetUserId <= 0, "Target user id must be grater than zero");
#endregion
var groups = GroupService.GetUserGroups(targetUserId);
foreach (var group in groups)
{
var groupUser = new GroupUser
{
GroupID = group.Id,
UserID = user.Id
};
user.GroupUsers.Add(groupUser);
}
this.Repository.Add(user, user.Id == 0);
#region Exit Method Tracing
if (log.IsDebugEnabled)
{
log.Debug("Completed " + this.GetType().ToString() + " - " + System.Reflection.MethodBase.GetCurrentMethod().ToString());
}
#endregion
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.