[8042] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11185] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8042] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Clients.Common;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Clients.Access.Administration {
|
---|
| 28 | [Item("AccessAdministrationClient", "AccessAdministration client.")]
|
---|
| 29 | public class AccessAdministrationClient : IContent {
|
---|
| 30 | private static AccessAdministrationClient instance;
|
---|
| 31 | public static AccessAdministrationClient Instance {
|
---|
| 32 | get {
|
---|
| 33 | if (instance == null) instance = new AccessAdministrationClient();
|
---|
| 34 | return instance;
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | #region Properties
|
---|
| 39 | private ItemList<User> users;
|
---|
| 40 | public ItemList<User> Users {
|
---|
| 41 | get {
|
---|
| 42 | return users;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | public ItemList<UserGroup> Groups { get; set; }
|
---|
| 47 | public ItemList<Role> Roles { get; set; }
|
---|
| 48 | #endregion
|
---|
| 49 |
|
---|
| 50 | private AccessAdministrationClient() { }
|
---|
| 51 |
|
---|
| 52 | #region Refresh
|
---|
| 53 | public void RefreshUsers() {
|
---|
| 54 | users = new ItemList<User>();
|
---|
| 55 | users.AddRange(CallAccessService<ItemList<User>>(s => new ItemList<User>(s.GetAllUsers())));
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public void RefreshUsersAsync(Action<Exception> exceptionCallback) {
|
---|
| 59 | ExecuteActionAsync(RefreshUsers, exceptionCallback);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public void RefreshUserGroups() {
|
---|
| 63 | Groups = new ItemList<UserGroup>();
|
---|
| 64 | Groups.AddRange(CallAccessService<ItemList<UserGroup>>(s => new ItemList<UserGroup>(s.GetAllUserGroups())));
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public void RefreshUserGroupsAsync(Action<Exception> exceptionCallback) {
|
---|
| 68 | ExecuteActionAsync(RefreshUserGroups, exceptionCallback);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public void RefreshRoles() {
|
---|
| 72 | Roles = new ItemList<Role>();
|
---|
| 73 | Roles.AddRange(CallAccessService<ItemList<Role>>(s => new ItemList<Role>(s.GetRoles())));
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | public void RefreshRolesAsync(Action<Exception> exceptionCallback) {
|
---|
| 77 | ExecuteActionAsync(RefreshRoles, exceptionCallback);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | #endregion
|
---|
| 81 |
|
---|
| 82 | #region Store
|
---|
| 83 | public void StoreUsers() {
|
---|
| 84 | foreach (User u in users) {
|
---|
| 85 | if (u.Modified) {
|
---|
| 86 | if (u.Id == Guid.Empty) {
|
---|
| 87 | CallAccessService(s => u.Id = s.AddUser(u).Id);
|
---|
| 88 | } else {
|
---|
| 89 | CallAccessService(s => s.UpdateUser(u));
|
---|
| 90 | }
|
---|
| 91 | u.SetUnmodified();
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | public void StoreUsersAsync(Action<Exception> exceptionCallback) {
|
---|
| 97 | ExecuteActionAsync(StoreUsers, exceptionCallback);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public void StoreUserGroups() {
|
---|
| 101 | foreach (UserGroup g in Groups) {
|
---|
| 102 | if (g.Modified) {
|
---|
| 103 | if (g.Id == Guid.Empty) {
|
---|
| 104 | CallAccessService(s => g.Id = s.AddUserGroup(g));
|
---|
| 105 | } else {
|
---|
| 106 | CallAccessService(s => s.UpdateUserGroup(g));
|
---|
| 107 | }
|
---|
| 108 | g.SetUnmodified();
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | public void StoreUserGroupsAsync(Action<Exception> exceptionCallback) {
|
---|
| 114 | ExecuteActionAsync(StoreUserGroups, exceptionCallback);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | public void StoreRoles() {
|
---|
| 118 | foreach (Role g in Roles) {
|
---|
| 119 | if (g.Modified) {
|
---|
| 120 | CallAccessService(s => s.AddRole(g));
|
---|
| 121 | g.SetUnmodified();
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | public void StoreRolesAsync(Action<Exception> exceptionCallback) {
|
---|
| 127 | ExecuteActionAsync(StoreRoles, exceptionCallback);
|
---|
| 128 | }
|
---|
| 129 | #endregion
|
---|
| 130 |
|
---|
| 131 | #region Delete
|
---|
| 132 | public void DeleteUser(User u) {
|
---|
| 133 | CallAccessService(s => s.DeleteUser(u));
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | public void DeleteUserAsync(User u, Action<Exception> exceptionCallback) {
|
---|
| 137 | Action deleteUserAction = new Action(delegate { DeleteUser(u); });
|
---|
| 138 | ExecuteActionAsync(deleteUserAction, exceptionCallback);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | public void DeleteUserGroup(UserGroup u) {
|
---|
| 142 | CallAccessService(s => s.DeleteUserGroup(u));
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | public void DeleteUserGroupAsync(UserGroup u, Action<Exception> exceptionCallback) {
|
---|
| 146 | Action deleteUserGroupAction = new Action(delegate { DeleteUserGroup(u); });
|
---|
| 147 | ExecuteActionAsync(deleteUserGroupAction, exceptionCallback);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | public void DeleteRole(Role u) {
|
---|
| 151 | CallAccessService(s => s.DeleteRole(u));
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | public void DeleteRoleAsync(Role u, Action<Exception> exceptionCallback) {
|
---|
| 155 | Action deleteRoleAction = new Action(delegate { DeleteRole(u); });
|
---|
| 156 | ExecuteActionAsync(deleteRoleAction, exceptionCallback);
|
---|
| 157 | }
|
---|
| 158 | #endregion
|
---|
| 159 |
|
---|
| 160 | public void ExecuteActionAsync(Action action, Action<Exception> exceptionCallback) {
|
---|
| 161 | var call = new Func<Exception>(delegate() {
|
---|
| 162 | try {
|
---|
| 163 | OnRefreshing();
|
---|
| 164 | action();
|
---|
| 165 | }
|
---|
| 166 | catch (Exception ex) {
|
---|
| 167 | return ex;
|
---|
| 168 | }
|
---|
| 169 | finally {
|
---|
| 170 | OnRefreshed();
|
---|
| 171 | }
|
---|
| 172 | return null;
|
---|
| 173 | });
|
---|
| 174 | call.BeginInvoke(delegate(IAsyncResult result) {
|
---|
| 175 | Exception ex = call.EndInvoke(result);
|
---|
| 176 | if (ex != null) exceptionCallback(ex);
|
---|
| 177 | }, null);
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | #region Events
|
---|
| 181 | public event EventHandler Refreshing;
|
---|
| 182 | private void OnRefreshing() {
|
---|
| 183 | EventHandler handler = Refreshing;
|
---|
| 184 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 185 | }
|
---|
| 186 | public event EventHandler Refreshed;
|
---|
| 187 | private void OnRefreshed() {
|
---|
| 188 | EventHandler handler = Refreshed;
|
---|
| 189 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 190 | }
|
---|
| 191 | #endregion
|
---|
| 192 |
|
---|
| 193 | #region Helpers
|
---|
| 194 | public static void CallAccessService(Action<IAccessService> call) {
|
---|
| 195 | AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
|
---|
| 196 | try {
|
---|
| 197 | call(client);
|
---|
| 198 | }
|
---|
| 199 | finally {
|
---|
| 200 | try {
|
---|
| 201 | client.Close();
|
---|
| 202 | }
|
---|
| 203 | catch (Exception) {
|
---|
| 204 | client.Abort();
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | public static T CallAccessService<T>(Func<IAccessService, T> call) {
|
---|
| 209 | AccessServiceClient client = ClientFactory.CreateClient<AccessServiceClient, IAccessService>();
|
---|
| 210 | try {
|
---|
| 211 | return call(client);
|
---|
| 212 | }
|
---|
| 213 | finally {
|
---|
| 214 | try {
|
---|
| 215 | client.Close();
|
---|
| 216 | }
|
---|
| 217 | catch (Exception) {
|
---|
| 218 | client.Abort();
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 | #endregion
|
---|
| 223 | }
|
---|
| 224 | }
|
---|