aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.UsersAndRoles/ViewModels/MainViewVM.cs
blob: 75e4af225915f88368bd2cda45d58c576cce0c7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.MachineStudio.Common;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.UsersAndRoles.Navigation;
using Tango.MachineStudio.UsersAndRoles.Providers;
using Tango.SharedUI;
using System.Data.Entity;
using Tango.BL.Builders;
using Tango.BL.ActionLogs;
using Tango.BL.DTO;
using Tango.BL.Enumerations;
using Tango.MachineStudio.Common.Authentication;

namespace Tango.MachineStudio.UsersAndRoles.ViewModels
{
    public class MainViewVM : StudioViewModel
    {
        private ObservablesContext _organizationsContext;
        private ObservablesContext _manageContext;
        private ObservablesContext _userContext;
        private UsersAndRolesNavigationManager _navigation;
        private INotificationProvider _notification;
        private IActionLogManager _actionLogManager;
        private OrganizationDTO _organizationBeforeSave;
        private IAuthenticationProvider _authenticationProvider;
        private UserDTO _userBeforeSave;

        private ObservableCollection<Organization> _organizations;
        public ObservableCollection<Organization> Organizations
        {
            get { return _organizations; }
            set { _organizations = value; RaisePropertyChangedAuto(); }
        }

        private Organization _selectedOrganization;
        public Organization SelectedOrganization
        {
            get { return _selectedOrganization; }
            set { _selectedOrganization = value; RaisePropertyChangedAuto(); }
        }

        private Organization _managedOrganization;
        public Organization ManagedOrganization
        {
            get { return _managedOrganization; }
            set { _managedOrganization = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<Role> _roles;
        public ObservableCollection<Role> Roles
        {
            get { return _roles; }
            set { _roles = value; RaisePropertyChangedAuto(); }
        }

        private ObservableCollection<Role> _managedUserRoles;
        public ObservableCollection<Role> ManagedUserRoles
        {
            get { return _managedUserRoles; }
            set { _managedUserRoles = value; RaisePropertyChangedAuto(); }
        }

        private User _selectedUser;
        public User SelectedUser
        {
            get { return _selectedUser; }
            set { _selectedUser = value; RaisePropertyChangedAuto(); }
        }

        private User _managedUser;
        public User ManagedUser
        {
            get { return _managedUser; }
            set { _managedUser = value; RaisePropertyChangedAuto(); }
        }

        private Place _selectedUserPlace;
        public Place SelectedUserPlace
        {
            get { return _selectedUserPlace; }
            set
            {
                _selectedUserPlace = value;

                if (_selectedUserPlace != null && _selectedUserPlace.Address != null)
                {
                    SetUserPlace(value);
                }
            }
        }

        private Place _selectedOrganizationPlace;
        public Place SelectedOrganizationPlace
        {
            get { return _selectedOrganizationPlace; }
            set
            {
                _selectedOrganizationPlace = value;

                if (_selectedOrganizationPlace != null && _selectedOrganizationPlace.Address != null)
                {
                    SetOrganizationPlace(value);
                }
            }
        }

        private bool _showDeleted;
        public bool ShowDeleted
        {
            get { return _showDeleted; }
            set
            {
                _showDeleted = value;
                if (_showDeleted)
                {
                    //ShowDeletedUsers();
                }
            }
        }


        public RelayCommand ManageOrganizationCommand { get; set; }

        public RelayCommand BackToOrganizationsCommand { get; set; }

        public RelayCommand ManageUserCommand { get; set; }

        public RelayCommand SaveOrganizationCommand { get; set; }

        public RelayCommand AddOrganizationCommand { get; set; }

        public RelayCommand RemoveOrganizationCommand { get; set; }

        public RelayCommand BackToManagedOrganizationCommand { get; set; }

        public RelayCommand<Role> RemoveRoleCommand { get; set; }

        public RelayCommand SaveManagedUserCommand { get; set; }

        public RelayCommand RestoreAndSaveManagedUserCommand { get; set; }

        public RelayCommand AddUserCommand { get; set; }

        public RelayCommand RemoveUserCommand { get; set; }

        public MainViewVM(UsersAndRolesNavigationManager navigation, INotificationProvider notification, IActionLogManager actionLogManager, IAuthenticationProvider authenticationProvider)
        {
            _navigation = navigation;
            _notification = notification;
            _actionLogManager = actionLogManager;
            _authenticationProvider = authenticationProvider;

            ManageOrganizationCommand = new RelayCommand(LoadSelectedOrganization, () => SelectedOrganization != null);
            BackToOrganizationsCommand = new RelayCommand(BackToOrganizations);
            ManageUserCommand = new RelayCommand(LoadSelectedUser, () => SelectedUser != null);
            SaveOrganizationCommand = new RelayCommand(SaveOrganization);
            AddOrganizationCommand = new RelayCommand(AddOrganization);
            RemoveOrganizationCommand = new RelayCommand(RemoveOrganization, () => SelectedOrganization != null);
            BackToManagedOrganizationCommand = new RelayCommand(BackToManagedOrganization);
            RemoveRoleCommand = new RelayCommand<Role>(RemoveUserRole);
            SaveManagedUserCommand = new RelayCommand(SaveManagedUser);
            RestoreAndSaveManagedUserCommand = new RelayCommand(RestoreAndSaveManagedUser);
            AddUserCommand = new RelayCommand(AddNewUser);
            RemoveUserCommand = new RelayCommand(RemoveSelectedUser, () => SelectedUser != null);
            _showDeleted = false;
        }

        public override void OnApplicationReady()
        {
            LoadOrganizations();
        }

        private async void AddOrganization()
        {
            String name = _notification.ShowTextInput("Enter organization name", "Name");

            if (!String.IsNullOrWhiteSpace(name))
            {
                using (_notification.PushTaskItem("Adding new organization..."))
                {
                    Organization org = new Organization();
                    org.Name = name;
                    org.Address = new Address();
                    org.Contact = new Contact();
                    _organizationsContext.Organizations.Add(org);
                    await org.SaveAsync(_organizationsContext);
                    _actionLogManager.InsertLog(ActionLogType.OrganizationCreated, _authenticationProvider.CurrentUser, org.Name, org, "Organization created using Machine Studio.");
                    Organizations = _organizationsContext.Organizations.ToObservableCollection();
                    SelectedOrganization = org;
                    LoadSelectedOrganization();
                }
            }
        }

        private async void RemoveOrganization()
        {
            if (_notification.ShowQuestion("Are you sure you want to remove " + SelectedOrganization.Name + " organization?"))
            {
                Organization selectedToRemoveOrganization = null;
                await Task.Factory.StartNew(() =>
                {
                    _manageContext = ObservablesContext.CreateDefault();
                    selectedToRemoveOrganization = new OrganizationBuilder(_manageContext).Set(SelectedOrganization.Guid).WithUsers(true).WithMachines().WithSites().Build();
                });
                if (selectedToRemoveOrganization != null && selectedToRemoveOrganization.Machines.Count > 0)
                {
                    _notification.ShowError("The organization cannot be removed as it contains Machines.");
                    return;
                }
                if (selectedToRemoveOrganization != null && selectedToRemoveOrganization.Sites.Count > 0)
                {
                    _notification.ShowError("The organization cannot be removed as it contains Sites.");
                    return;
                }
                if (selectedToRemoveOrganization != null && selectedToRemoveOrganization.Users.Count(x => x.OrganizationGuid == SelectedOrganization.Guid && x.Deleted == false) > 0)
                {
                    _notification.ShowError("The organization cannot be removed as it contains active users.");
                    return;
                }

                using (_notification.PushTaskItem("Removing organization..."))
                {
                    await SelectedOrganization.DeleteCascadeAsync(_organizationsContext);
                    _actionLogManager.InsertLog(ActionLogType.OrganizationDeleted, _authenticationProvider.CurrentUser, SelectedOrganization.Name, SelectedOrganization, "Organization deleted using Machine Studio.");
                    await LoadOrganizations();
                }
            }
        }

        private async void SaveOrganization()
        {
            using (_notification.PushTaskItem("Saving organization address and contact..."))
            {
                var organizationAfter = OrganizationDTO.FromObservable(ManagedOrganization);

                await ManagedOrganization.SaveAsync(_manageContext);
                _actionLogManager.InsertLog(ActionLogType.OrganizationSaved, _authenticationProvider.CurrentUser, ManagedOrganization.Name, _organizationBeforeSave, organizationAfter, "Organization saved using Machine Studio.");

                _organizationBeforeSave = organizationAfter;

                await LoadOrganizations();
                SelectedOrganization = Organizations.SingleOrDefault(x => x.Guid == ManagedOrganization.Guid);
            }
        }

        private async void LoadSelectedUser()
        {
            using (_notification.PushTaskItem("Loading user details..."))
            {
                await Task.Factory.StartNew(() =>
                {
                    _userContext = ObservablesContext.CreateDefault();

                    Roles = new RolesCollectionBuilder(_userContext).SetAll().WithPermission().Build();
                    ManagedUser = new UserBuilder(_userContext).WithDeleted().Set(SelectedUser.Guid).WithRolesAndPermissions().Build();
                    ManagedUserRoles = ManagedUser.Roles.ToObservableCollection();

                    _userBeforeSave = UserDTO.FromObservable(ManagedUser);
                });

                _navigation.NavigateTo(UsersAndRolesNavigationView.UserManagementView);
            }
        }

        private void BackToOrganizations()
        {
            _navigation.NavigateTo(UsersAndRolesNavigationView.OrganizationSelectionView);
        }

        private void BackToManagedOrganization()
        {
            _navigation.NavigateTo(UsersAndRolesNavigationView.OrganizationManagementView);
        }

        private void RemoveUserRole(Role role)
        {
            ManagedUserRoles.Remove(role);

            foreach (var userRole in ManagedUser.UsersRoles.Where(x => x.Role == role).ToList())
            {
                _userContext.UsersRoles.Remove(userRole);
            }
        }

        private async void SaveManagedUser()
        {
            try
            {
                ManagedUser.Validate(_userContext);

                if (ManagedUser.Roles.GroupBy(x => x.RoleEnum).Any(x => x.Count() > 1))
                {
                    throw new InvalidOperationException("Cannot save user with duplicate roles.");
                }
            }
            catch (Exception ex)
            {
                _notification.ShowError(ex.Message);
                return;
            }

            using (_notification.PushTaskItem("Saving user details..."))
            {
                var userAfter = UserDTO.FromObservable(ManagedUser);
                await ManagedUser.SaveAsync(_userContext);
                _actionLogManager.InsertLog(ActionLogType.UserSaved, _authenticationProvider.CurrentUser, ManagedUser.Email, _userBeforeSave, userAfter, "User saved using Machine Studio.");
                _userBeforeSave = userAfter;
                LoadSelectedOrganization();
            }
        }

        private async void RestoreAndSaveManagedUser()
        {
            try
            {
                ManagedUser.Validate(_userContext);

                if (ManagedUser.Roles.GroupBy(x => x.RoleEnum).Any(x => x.Count() > 1))
                {
                    throw new InvalidOperationException("Cannot save user with duplicate roles.");
                }
            }
            catch (Exception ex)
            {
                _notification.ShowError(ex.Message);
                return;
            }
            if (_notification.ShowQuestion("Are you sure you wish to re-activate this account?"))
            {
                using (_notification.PushTaskItem("Saving user details..."))
                {
                    ManagedUser.Deleted = false;

                    var userAfter = UserDTO.FromObservable(ManagedUser);
                    await ManagedUser.SaveAsync(_userContext);
                    _actionLogManager.InsertLog(ActionLogType.UserRestored, _authenticationProvider.CurrentUser, ManagedUser.Email, _userBeforeSave, userAfter, "User restored using Machine Studio.");
                    _userBeforeSave = userAfter;
                    LoadSelectedOrganization();
                }
            }
        }

        private async void LoadSelectedOrganization()
        {
            using (_notification.PushTaskItem("Loading organization..."))
            {
                await Task.Factory.StartNew(() =>
                {
                    _manageContext = ObservablesContext.CreateDefault();

                    ManagedOrganization = new OrganizationBuilder(_manageContext).Set(SelectedOrganization.Guid).WithUsers(true).Build();

                    _organizationBeforeSave = OrganizationDTO.FromObservable(ManagedOrganization);
                });

                _navigation.NavigateTo(UsersAndRolesNavigationView.OrganizationManagementView);
            }
        }

        private Task LoadOrganizations()
        {
            return Task.Factory.StartNew(() =>
            {
                _organizationsContext = ObservablesContext.CreateDefault();

                Organizations = new OrganizationsCollectionBuilder(_organizationsContext).SetAll().WithUsers().WithMachines().Build();
            });
        }

        public void OnDropRole(Role role)
        {
            ManagedUser.UsersRoles.Add(new UsersRole()
            {
                Role = role,
                RoleGuid = role.Guid,
                User = ManagedUser,
                UserGuid = ManagedUser.Guid,
            });

            ManagedUserRoles.Add(role);
        }

        private async void RemoveSelectedUser()
        {
            if (_notification.ShowQuestion("Are you sure you want to remove the selected user?"))
            {
                using (_notification.PushTaskItem("Removing user..."))
                {
                    await SelectedUser.DeleteCascadeAsync(_manageContext);
                    _actionLogManager.InsertLog(ActionLogType.UserDeleted, _authenticationProvider.CurrentUser, SelectedUser.Email, SelectedUser, "User deleted using Machine Studio.");
                    LoadSelectedOrganization();
                }
            }
        }

        private void AddNewUser()
        {
            _notification.ShowModalDialog<UserCreationDialogVM>(async (vm) =>
            {
                User user = new User();
                user.Email = vm.Email;
                user.Password = User.GetPasswordHash(vm.Password);
                user.PasswordChangeRequired = true;
                user.Contact = new Contact()
                {
                    FirstName = vm.FirstName,
                    LastName = vm.LastName,
                    Email = vm.Email,
                };

                user.Address = new Address();

                user.UsersRoles.Add(new UsersRole()
                {
                    User = user,
                    Role = _manageContext.Roles.SingleOrDefault(x => x.Code == (int)BL.Enumerations.Roles.User)
                });

                user.UsersRoles.Add(new UsersRole()
                {
                    User = user,
                    Role = _manageContext.Roles.SingleOrDefault(x => x.Code == (int)BL.Enumerations.Roles.MachineStudioUser)
                });

                user.UsersRoles.Add(new UsersRole()
                {
                    User = user,
                    Role = _manageContext.Roles.SingleOrDefault(x => x.Code == (int)BL.Enumerations.Roles.PPCUser)
                });

                try
                {
                    if (!user.Validate(_manageContext))
                    {
                        _notification.ShowError(String.Join(Environment.NewLine, user.ValidationErrors));
                        return;
                    }
                }
                catch (Exception ex)
                {
                    _notification.ShowError(ex.Message);
                    return;
                }

                ManagedOrganization.Users.Add(user);

                using (_notification.PushTaskItem("Adding new user..."))
                {
                    await ManagedOrganization.SaveAsync(_manageContext);
                    _actionLogManager.InsertLog(ActionLogType.UserCreated, _authenticationProvider.CurrentUser, user.Email, user, "User created using Machine Studio.");
                    await LoadOrganizations();
                    SelectedOrganization = Organizations.SingleOrDefault(x => x.Guid == ManagedOrganization.Guid);
                    SelectedUser = user;
                    LoadSelectedUser();
                }
            });
        }

        private void SetUserPlace(Place place)
        {
            ManagedUser.Address.AddressString = place.Address.Road;
            ManagedUser.Address.City = place.Address.City;
            ManagedUser.Address.Country = place.Address.Country;
            ManagedUser.Address.CountryCode = place.Address.CountryCode;
            ManagedUser.Address.Locality = place.Address.CountryCode;
            ManagedUser.Address.PostalCode = place.Address.PostalCode;
            ManagedUser.Address.State = place.Address.State;
        }

        private void SetOrganizationPlace(Place place)
        {
            ManagedOrganization.Address.AddressString = place.Address.Road;
            ManagedOrganization.Address.City = place.Address.City;
            ManagedOrganization.Address.Country = place.Address.Country;
            ManagedOrganization.Address.CountryCode = place.Address.CountryCode;
            ManagedOrganization.Address.Locality = place.Address.CountryCode;
            ManagedOrganization.Address.PostalCode = place.Address.PostalCode;
            ManagedOrganization.Address.State = place.Address.State;
        }

        protected override void RaisePropertyChangedAuto([CallerMemberName] string caller = null)
        {
            base.RaisePropertyChangedAuto(caller);
            InvalidateRelayCommands();
        }
    }
}