blob: 334c8c263a73e9c3bba10fd02f3dcd13481d7f16 (
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
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.FSE.Common;
using Tango.FSE.Common.Navigation;
using Tango.FSE.UsersAndRoles.Navigation;
using Tango.FSE.UsersAndRoles.Views;
using static Tango.FSE.UsersAndRoles.ViewModels.OrganizationUsersViewVM;
namespace Tango.FSE.UsersAndRoles.ViewModels
{
public class OrganizationUsersViewVM : UsersAndRolesViewModel, INavigationObjectReceiver<NavigationObject>
{
public class NavigationObject
{
public Organization Organization { get; set; }
public User SelectedUser { get; set; }
}
private bool _requiresReloading;
private User _initialUser;
private Organization _organization;
/// <summary>
/// Gets or sets the organization.
/// </summary>
public Organization Organization
{
get { return _organization; }
set { _organization = value; RaisePropertyChangedAuto(); }
}
private ObservableCollection<User> _users;
/// <summary>
/// Gets or sets the users.
/// </summary>
public ObservableCollection<User> Users
{
get { return _users; }
set { _users = value; RaisePropertyChangedAuto(); }
}
private User _selectedUser;
public User SelectedUser
{
get { return _selectedUser; }
set { _selectedUser = value; RaisePropertyChangedAuto(); }
}
/// <summary>
/// Gets or sets the users view source.
/// </summary>
public ICollectionView UsersView { get; set; }
private String _filter;
/// <summary>
/// Gets or sets the users search filter.
/// </summary>
public String Filter
{
get { return _filter; }
set { _filter = value; RaisePropertyChangedAuto(); OnFilterChanged(); }
}
private bool _showDeleted;
public bool ShowDeleted
{
get { return _showDeleted; }
set { _showDeleted = value; RaisePropertyChangedAuto(); UsersView?.Refresh(); }
}
/// <summary>
/// Navigates to the user details view with the specified user.
/// </summary>
public RelayCommand<User> EditUserCommand { get; set; }
/// <summary>
/// Removes the specified user.
/// </summary>
public RelayCommand<User> RemoveUserCommand { get; set; }
/// <summary>
/// Adds a new user.
/// </summary>
public RelayCommand AddUserCommand { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="OrganizationUsersViewVM"/> class.
/// </summary>
public OrganizationUsersViewVM()
{
Users = new ObservableCollection<User>();
EditUserCommand = new RelayCommand<User>(EditUser);
RemoveUserCommand = new RelayCommand<User>(SuspendUser);
AddUserCommand = new RelayCommand(AddUser, () => IsFree);
}
private void AddUser()
{
ModularNavigationManager.NavigateTo(UsersAndRolesView.UserDetailsView, new UserDetailsViewVM.NavigationObject()
{
IsNewUser = true,
Organization = Organization
});
}
private async void SuspendUser(User user)
{
if (await NotificationProvider.ShowWarningQuestion($"Are you sure you want to suspend '{user.Contact.FullName}' ?"))
{
try
{
IsFree = false;
using (NotificationProvider.PushTaskItem("Suspending account..."))
{
var updatedUser = await Services.OrganizationsService.SuspendUser(user);
Users.Replace(user, updatedUser);
}
await NotificationProvider.ShowSuccess("The selected user is now suspended and can be reactivated.");
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error suspending user '{user.Email}'.");
await NotificationProvider.ShowError($"Error suspending the selected user\n{ex.FlattenMessage()}");
}
finally
{
IsFree = true;
}
}
}
private void EditUser(User user)
{
ModularNavigationManager.NavigateTo(UsersAndRolesView.UserDetailsView, new UserDetailsViewVM.NavigationObject()
{
User = user
});
}
private async Task LoadUsers()
{
if (Organization != null)
{
try
{
IsFree = false;
var users = await Services.OrganizationsService.GetOrganizationUsers(Organization.Guid, true);
var usersCollection = new ObservableCollection<User>(users);
UsersView = CollectionViewSource.GetDefaultView(usersCollection);
UsersView.Filter = FilterUsers;
Users = usersCollection;
}
catch (Exception ex)
{
LogManager.Log(ex, $"Error loading organization users for organization '{Organization.Name}'.");
await NotificationProvider.ShowError($"Error loading the selected organization users.\n{ex.FlattenMessage()}");
await NavigationManager.NavigateBack();
}
finally
{
IsFree = true;
}
}
else
{
await NotificationProvider.ShowError("No organization selected.");
}
}
private void OnFilterChanged()
{
UsersView?.Refresh();
}
private bool FilterUsers(object obj)
{
var user = obj as User;
String filter = Filter ?? String.Empty;
if (user != null)
{
return (!filter.IsNotNullOrEmpty() || user.Contact.FullName.ToLower().Contains(filter.ToLower()) || user.Email.ToLower().Contains(filter.ToLower())) && (!user.Deleted || ShowDeleted);
}
return true;
}
/// <summary>
/// Called when the navigation system has navigated to this VM view.
/// </summary>
public async override void OnNavigatedTo()
{
base.OnNavigatedTo();
if (_requiresReloading)
{
await LoadUsers();
if (_initialUser != null)
{
SelectedUser = Users.SingleOrDefault(x => x.Guid == _initialUser.Guid);
}
}
_requiresReloading = false;
_initialUser = null;
}
/// <summary>
/// Called when navigation system is going to navigate to this instance with the specified object.
/// </summary>
/// <param name="obj">The object.</param>
public void OnNavigatedToWithObject(NavigationObject obj)
{
Organization = obj.Organization;
_initialUser = obj.SelectedUser;
_requiresReloading = true;
}
}
}
|