Permalink
Browse files

* Move the singleton service behavior attribute to AbstractUserContex…

…tAwareService

* Add a couple new finders for hosts.
* Add access flag management WCF endpoints.
  • Loading branch information...
1 parent f6bb7b1 commit 7baf72535418938e826661142babe9ad76386a02 @kog kog committed Apr 12, 2012
@@ -242,6 +242,11 @@ public KnownHost FindHostByAccessToken(string token)
x.AccessToken.Equals(token));
}
+ public KnownHost FindHostById(int id)
+ {
+ return _cloaks.FirstOrDefault(x => x.Id == id);
+ }
+
#endregion
}
}
@@ -17,6 +17,7 @@ namespace SpikeLite.IPC.WebHost
/// <summary>
/// Provides a service base class that is "user context" aware - namely, it can pull from a predetermined incoming property. Mostly a convenience.
/// </summary>
+ [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public abstract class AbstractUserContextAwareService : IConfigurableServiceHost
{
/// <summary>
@@ -83,12 +83,49 @@ public interface IAccountService : IConfigurableServiceHost
[SecuredOperation("manageUsers")]
void RevokeAccessToken(string accessToken);
+ /// <summary>
+ /// Gets the set of <see cref="AccessFlag"/> known to the system.
+ /// </summary>
+ ///
+ /// <returns>An array of all the known <see cref="AccessFlag"/> within the system.</returns>
+ [OperationContract]
+ [SecuredOperation]
+ AccessFlag[] GetAllKnownAccessFlags();
+
+ /// <summary>
+ /// Creates a new access flag within the system.
+ /// </summary>
+ ///
+ /// <param name="name">The name of the access flag. Should be an alpha string.</param>
+ /// <param name="description">A textual description of the access flag.</param>
+ [OperationContract]
+ [SecuredOperation("manageUsers")]
+ void CreateAccessFlag(string name, string description);
+
+ /// <summary>
+ /// Adds a specific flag to a user.
+ /// </summary>
+ ///
+ /// <param name="userId">The ID of the user to add the flag to.</param>
+ /// <param name="accessFlag">The IDs of the flag to add to the given user.</param>
+ [OperationContract]
+ [SecuredOperation("manageUsers")]
+ void AddAccessFlagToUser(int userId, int accessFlag);
+
+ /// <summary>
+ /// Revokes an access flag from a user.
+ /// </summary>
+ ///
+ /// <param name="userId">The ID of the user to revoke the flag from.</param>
+ /// <param name="accessFlag">The access flag to revoke.</param>
+ [OperationContract]
+ [SecuredOperation("manageUsers")]
+ void RevokeAccessFlagFromUser(int userId, int accessFlag);
}
/// <summary>
/// Provides a concrete implementation of <see cref="IAccountService"/>.
/// </summary>
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class AccountService : AbstractUserContextAwareService, IAccountService
{
public override void Configure()
@@ -141,8 +178,8 @@ public DateTime GetTokenExpiration()
public TransportKnownHost[] GetAllUsers()
{
- var knownHostDao = GetBean<IKnownHostDao>("KnownHostDao");
- return Mapper.Map<IList<KnownHost>, TransportKnownHost[]>(knownHostDao.FindAll());
+ var authenticationManager = GetBean<IrcAuthenticationModule>("IrcAuthenticationModule");
+ return Mapper.Map<IEnumerable<KnownHost>, TransportKnownHost[]>(authenticationManager.GetHosts());
}
public TransportKnownHost GetUserById(int id)
@@ -162,5 +199,61 @@ public void RevokeAccessToken(string accessToken)
authenticationManager.UpdateHost(host);
}
}
+
+ public AccessFlag[] GetAllKnownAccessFlags()
+ {
+ var accessFlagDao = GetBean<IAccessFlagDao>("AccessFlagDao");
+ return accessFlagDao.FindAll().ToArray();
+ }
+
+ public void CreateAccessFlag(string name, string description)
+ {
+ var accessFlagDao = GetBean<IAccessFlagDao>("AccessFlagDao");
+
+ if (!accessFlagDao.FindAll().Any(x => x.Flag.Equals(name, StringComparison.InvariantCultureIgnoreCase)))
+ {
+ accessFlagDao.SaveOrUpdate(accessFlagDao.CreateFlag(name, description));
+ }
+ }
+
+ public void AddAccessFlagToUser(int userId, int accessFlag)
+ {
+ var authenticationManager = GetBean<IrcAuthenticationModule>("IrcAuthenticationModule");
+ var accessFlagDao = GetBean<IAccessFlagDao>("AccessFlagDao");
+ var knownHost = authenticationManager.FindHostById(userId);
+
+ // Make sure we know the host corresponding to the id.
+ if (null != knownHost)
+ {
+ // Make sure the access flag actually exists as well...
+ var flag = accessFlagDao.FindAll().FirstOrDefault(x => x.Id == accessFlag);
+
+ // Lastly, make sure the flag is not already set.
+ if (null != flag && ! knownHost.AccessFlags.Any(x => x.Id == accessFlag))
+ {
+ knownHost.AccessFlags.Add(flag);
+ authenticationManager.UpdateHost(knownHost);
+ }
+ }
+ }
+
+ public void RevokeAccessFlagFromUser(int userId, int accessFlag)
+ {
+ var authenticationManager = GetBean<IrcAuthenticationModule>("IrcAuthenticationModule");
+ var accessFlagDao = GetBean<IAccessFlagDao>("AccessFlagDao");
+ var knownHost = authenticationManager.FindHostById(userId);
+
+ // Make sure we know the host corresponding to the id.
+ if (null != knownHost)
+ {
+ // Make sure that we know what this flag is.
+ if (accessFlagDao.FindAll().Any(x => x.Id == accessFlag))
+ {
+ // Try and remove the flag - if it's not set, we should have a no-op here.
+ knownHost.AccessFlags.Remove(knownHost.AccessFlags.FirstOrDefault(x => x.Id == accessFlag));
+ authenticationManager.UpdateHost(knownHost);
+ }
+ }
+ }
}
}
@@ -67,8 +67,10 @@ public interface IFactoidService : IConfigurableServiceHost
}
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
- class FactoidService : AbstractUserContextAwareService, IFactoidService
+ /// <summary>
+ /// Provides a concrete implementation of <see cref="IFactoidService"/>.
+ /// </summary>
+ public class FactoidService : AbstractUserContextAwareService, IFactoidService
{
public override void Configure()
{
@@ -31,7 +31,6 @@ public interface IMessagingService : IConfigurableServiceHost
/// <summary>
/// Implements the <see cref="IMessagingService"/> contract, providing a concrete implementation of our service.
/// </summary>
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MessagingService : AbstractUserContextAwareService, IMessagingService
{
public void SendMessage(string channelTarget, string messageText)
@@ -29,7 +29,6 @@ public interface IPingService : IConfigurableServiceHost
/// <summary>
/// Implements our <see cref="IPingService"/> contract.
/// </summary>
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class PingService : AbstractUserContextAwareService, IPingService
{
public string Ping()

0 comments on commit 7baf725

Please sign in to comment.