Index: /branches/OKB (trunk integration)/HeuristicLab.Services.OKB/3.3/Query/QueryService.cs
===================================================================
--- /branches/OKB (trunk integration)/HeuristicLab.Services.OKB/3.3/Query/QueryService.cs	(revision 7913)
+++ /branches/OKB (trunk integration)/HeuristicLab.Services.OKB/3.3/Query/QueryService.cs	(revision 7914)
@@ -25,4 +25,5 @@
 using System.Linq;
 using System.ServiceModel;
+using HeuristicLab.Services.Access;
 using HeuristicLab.Services.OKB.DataAccess;
 using HeuristicLab.Services.OKB.Query.DataTransfer;
@@ -35,4 +36,7 @@
   [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
   public class QueryService : IQueryService {
+    IRoleVerifier roleVerifier = AccessServiceLocator.Instance.RoleVerifier;
+    IUserManager userManager = AccessServiceLocator.Instance.UserManager;
+
     public IEnumerable<Filter> GetFilters() {
       List<Filter> filters = new List<Filter>();
@@ -238,5 +242,5 @@
     public long GetNumberOfRuns(Filter filter) {
       using (OKBDataContext okb = new OKBDataContext()) {
-        return FilterRuns(okb.Runs, filter).LongCount();
+        return FilterRuns(okb.Runs, filter, okb).LongCount();
       }
     }
@@ -244,5 +248,5 @@
     public IEnumerable<long> GetRunIds(Filter filter) {
       using (OKBDataContext okb = new OKBDataContext()) {
-        return FilterRuns(okb.Runs, filter).Select(x => x.Id).ToArray();
+        return FilterRuns(okb.Runs, filter, okb).Select(x => x.Id).ToArray();
       }
     }
@@ -254,5 +258,5 @@
         okb.LoadOptions = dlo;
 
-        return okb.Runs.Where(x => ids.Contains(x.Id)).Select(x => Convert.ToDto(x, includeBinaryValues)).ToArray();
+        return FilterUnauthorizedRuns(okb.Runs.Where(x => ids.Contains(x.Id)).ToList(), okb).Select(x => Convert.ToDto(x, includeBinaryValues)).ToArray();
       }
     }
@@ -264,5 +268,5 @@
         okb.LoadOptions = dlo;
 
-        return okb.Runs.Where(x => ids.Contains(x.Id)).Select(x => Convert.ToDto(x, includeBinaryValues, valueNames)).ToArray();
+        return FilterUnauthorizedRuns(okb.Runs.Where(x => ids.Contains(x.Id)).ToList(), okb).Select(x => Convert.ToDto(x, includeBinaryValues, valueNames)).ToArray();
       }
     }
@@ -274,7 +278,53 @@
     }
 
-    private IQueryable<DataAccess.Run> FilterRuns(IQueryable<DataAccess.Run> runs, Filter filter) {
+    private List<DataAccess.Run> FilterRuns(IQueryable<DataAccess.Run> runs, Filter filter, OKBDataContext okb) {
       IFilter f = (IFilter)Activator.CreateInstance(Type.GetType(filter.FilterTypeName), filter);
-      return runs.Where(f.Expression);
+
+      var query = runs.Where(f.Expression);
+      List<DataAccess.Run> results = new List<DataAccess.Run>();
+
+      if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
+        results.AddRange(query);
+      } else {
+        foreach (DataAccess.Run r in query) {
+          if (IsAuthorizedForAlgorithm(r.AlgorithmId, okb) && IsAuthorizedForProblem(r.ProblemId, okb)) {
+            results.Add(r);
+          }
+        }
+      }
+      return results;
+    }
+
+    private List<DataAccess.Run> FilterUnauthorizedRuns(List<DataAccess.Run> runs, OKBDataContext okb) {
+      List<DataAccess.Run> results = new List<DataAccess.Run>();
+
+      if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
+        results.AddRange(runs);
+      } else {
+        foreach (DataAccess.Run r in runs) {
+          if (IsAuthorizedForAlgorithm(r.AlgorithmId, okb) && IsAuthorizedForProblem(r.ProblemId, okb)) {
+            results.Add(r);
+          }
+        }
+      }
+      return results;
+    }
+
+    private bool IsAuthorizedForAlgorithm(long algorithmId, OKBDataContext okb) {
+      var algUsers = okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId).Select(x => x.UserGroupId).ToList();
+
+      if (algUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, algUsers)) {
+        return true;
+      }
+      return false;
+    }
+
+    private bool IsAuthorizedForProblem(long problemId, OKBDataContext okb) {
+      var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == problemId).Select(x => x.UserGroupId).ToList();
+
+      if (problemUsers.Count == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers)) {
+        return true;
+      }
+      return false;
     }
   }
Index: /branches/OKB (trunk integration)/HeuristicLab.Services.OKB/3.3/RunCreation/RunCreationService.cs
===================================================================
--- /branches/OKB (trunk integration)/HeuristicLab.Services.OKB/3.3/RunCreation/RunCreationService.cs	(revision 7913)
+++ /branches/OKB (trunk integration)/HeuristicLab.Services.OKB/3.3/RunCreation/RunCreationService.cs	(revision 7914)
@@ -34,4 +34,5 @@
   public class RunCreationService : IRunCreationService {
     IRoleVerifier roleVerifier = AccessServiceLocator.Instance.RoleVerifier;
+    IUserManager userManager = AccessServiceLocator.Instance.UserManager;
 
     public IEnumerable<DataTransfer.Algorithm> GetAlgorithms(string platformName) {
@@ -42,6 +43,20 @@
         dlo.LoadWith<Algorithm>(x => x.AlgorithmClass);
         dlo.LoadWith<Algorithm>(x => x.DataType);
+        dlo.LoadWith<Algorithm>(x => x.AlgorithmUsers);
         okb.LoadOptions = dlo;
-        return okb.Algorithms.Where(x => x.Platform.Name == platformName).Select(x => Convert.ToDto(x)).ToArray();
+
+        var query = okb.Algorithms.Where(x => x.Platform.Name == platformName);
+        List<Algorithm> results = new List<Algorithm>();
+
+        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
+          results.AddRange(query);
+        } else {
+          foreach (var alg in query) {
+            if (alg.AlgorithmUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, alg.AlgorithmUsers.Select(y => y.UserGroupId).ToList())) {
+              results.Add(alg);
+            }
+          }
+        }
+        return results.Select(x => Convert.ToDto(x)).ToArray();
       }
     }
@@ -51,5 +66,16 @@
 
       using (OKBDataContext okb = new OKBDataContext()) {
-        return okb.Algorithms.Where(x => x.Id == algorithmId).Select(x => x.BinaryData.Data.ToArray()).FirstOrDefault();
+        var result = okb.Algorithms.Where(x => x.Id == algorithmId).Select(x => x.BinaryData.Data.ToArray()).FirstOrDefault();
+
+        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
+          return result;
+        } else {
+          var algUsers = okb.AlgorithmUsers.Where(x => x.AlgorithmId == algorithmId);
+          if (algUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, algUsers.Select(y => y.UserGroupId).ToList())) {
+            return result;
+          } else {
+            return null;
+          }
+        }
       }
     }
@@ -62,6 +88,20 @@
         dlo.LoadWith<Problem>(x => x.ProblemClass);
         dlo.LoadWith<Problem>(x => x.DataType);
+        dlo.LoadWith<Problem>(x => x.ProblemUsers);
         okb.LoadOptions = dlo;
-        return okb.Problems.Where(x => x.Platform.Name == platformName).Select(x => Convert.ToDto(x)).ToArray();
+
+        var query = okb.Problems.Where(x => x.Platform.Name == platformName);
+        List<Problem> results = new List<Problem>();
+
+        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
+          results.AddRange(query);
+        } else {
+          foreach (var problem in query) {
+            if (problem.ProblemUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, problem.ProblemUsers.Select(y => y.UserGroupId).ToList())) {
+              results.Add(problem);
+            }
+          }
+        }
+        return results.Select(x => Convert.ToDto(x)).ToArray();
       }
     }
@@ -71,5 +111,16 @@
 
       using (OKBDataContext okb = new OKBDataContext()) {
-        return okb.Problems.Where(x => x.Id == problemId).Select(x => x.BinaryData.Data.ToArray()).FirstOrDefault();
+        var result = okb.Problems.Where(x => x.Id == problemId).Select(x => x.BinaryData.Data.ToArray()).FirstOrDefault();
+
+        if (roleVerifier.IsInRole(OKBRoles.OKBAdministrator)) {
+          return result;
+        } else {
+          var problemUsers = okb.ProblemUsers.Where(x => x.ProblemId == problemId);
+          if (problemUsers.Count() == 0 || userManager.VerifyUser(userManager.CurrentUserId, problemUsers.Select(y => y.UserGroupId).ToList())) {
+            return result;
+          } else {
+            return null;
+          }
+        }
       }
     }
