- Fixed bugs number 195 and 203
[public/netxms.git] / src / console / win32 / UserEditor.cpp
1 // UserEditor.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "nxcon.h"
6 #include "UserEditor.h"
7
8 #ifdef _DEBUG
9 #define new DEBUG_NEW
10 #undef THIS_FILE
11 static char THIS_FILE[] = __FILE__;
12 #endif
13
14 /////////////////////////////////////////////////////////////////////////////
15 // CUserEditor
16
17 IMPLEMENT_DYNCREATE(CUserEditor, CMDIChildWnd)
18
19 CUserEditor::CUserEditor()
20 {
21 m_dwCurrentUser = INVALID_UID;
22 m_iSortMode = theApp.GetProfileInt(_T("UserEditor"), _T("SortMode"), 0);
23 m_iSortDir = theApp.GetProfileInt(_T("UserEditor"), _T("SortDir"), 1);
24 }
25
26 CUserEditor::~CUserEditor()
27 {
28 theApp.WriteProfileInt(_T("UserEditor"), _T("SortMode"), m_iSortMode);
29 theApp.WriteProfileInt(_T("UserEditor"), _T("SortDir"), m_iSortDir);
30 }
31
32
33 BEGIN_MESSAGE_MAP(CUserEditor, CMDIChildWnd)
34 //{{AFX_MSG_MAP(CUserEditor)
35 ON_WM_CLOSE()
36 ON_WM_DESTROY()
37 ON_WM_CREATE()
38 ON_WM_SIZE()
39 ON_COMMAND(ID_VIEW_REFRESH, OnViewRefresh)
40 ON_COMMAND(ID_USER_CREATE_GROUP, OnUserCreateGroup)
41 ON_COMMAND(ID_USER_CREATE_USER, OnUserCreateUser)
42 ON_WM_SETFOCUS()
43 ON_COMMAND(ID_USER_PROPERTIES, OnUserProperties)
44 ON_COMMAND(ID_USER_DELETE, OnUserDelete)
45 ON_WM_CONTEXTMENU()
46 ON_UPDATE_COMMAND_UI(ID_USER_PROPERTIES, OnUpdateUserProperties)
47 ON_UPDATE_COMMAND_UI(ID_USER_DELETE, OnUpdateUserDelete)
48 ON_COMMAND(ID_USER_SETPASSWORD, OnUserSetpassword)
49 ON_UPDATE_COMMAND_UI(ID_USER_SETPASSWORD, OnUpdateUserSetpassword)
50 //}}AFX_MSG_MAP
51 ON_MESSAGE(NXCM_USERDB_CHANGE, OnUserDBChange)
52 ON_NOTIFY(NM_DBLCLK, IDC_LIST_VIEW, OnListViewDblClk)
53 ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_VIEW, OnListViewItemChange)
54 ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST_VIEW, OnListViewColumnClick)
55 END_MESSAGE_MAP()
56
57 /////////////////////////////////////////////////////////////////////////////
58 // CUserEditor message handlers
59
60 BOOL CUserEditor::PreCreateWindow(CREATESTRUCT& cs)
61 {
62 if (cs.lpszClass == NULL)
63 cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,
64 NULL,
65 GetSysColorBrush(COLOR_WINDOW),
66 AfxGetApp()->LoadIcon(IDI_USER_GROUP));
67 return CMDIChildWnd::PreCreateWindow(cs);
68 }
69
70
71 //
72 // WM_CLOSE message handler
73 //
74
75 void CUserEditor::OnClose()
76 {
77 DoRequestArg1(NXCUnlockUserDB, g_hSession, _T("Unlocking user database..."));
78 CMDIChildWnd::OnClose();
79 }
80
81
82 //
83 // WM_DESTROY message handler
84 //
85
86 void CUserEditor::OnDestroy()
87 {
88 SaveListCtrlColumns(m_wndListCtrl, _T("UserEditor"), _T("ListCtrl"));
89 theApp.OnViewDestroy(VIEW_USER_MANAGER, this);
90 CMDIChildWnd::OnDestroy();
91 }
92
93
94 //
95 // WM_CREATE message handler
96 //
97
98 int CUserEditor::OnCreate(LPCREATESTRUCT lpCreateStruct)
99 {
100 RECT rect;
101
102 if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
103 return -1;
104
105 theApp.OnViewCreate(VIEW_USER_MANAGER, this);
106
107 // Create list view control
108 GetClientRect(&rect);
109 m_wndListCtrl.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SHOWSELALWAYS | LVS_SINGLESEL | LVS_SHAREIMAGELISTS, rect, this, IDC_LIST_VIEW);
110 m_wndListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
111 m_wndListCtrl.SetHoverTime(0x7FFFFFFF);
112
113 // Create image list
114 m_imageList.Create(16, 16, ILC_COLOR8 | ILC_MASK, 8, 8);
115 m_imageList.Add(AfxGetApp()->LoadIcon(IDI_USER));
116 m_imageList.Add(AfxGetApp()->LoadIcon(IDI_USER_GROUP));
117 m_imageList.Add(AfxGetApp()->LoadIcon(IDI_EVERYONE));
118 m_imageList.Add(AfxGetApp()->LoadIcon(IDI_OVL_STATUS_CRITICAL));
119 m_iSortImageBase = m_imageList.GetImageCount();
120 m_imageList.Add(theApp.LoadIcon(IDI_SORT_UP));
121 m_imageList.Add(theApp.LoadIcon(IDI_SORT_DOWN));
122 m_imageList.SetOverlayImage(3, 1);
123 m_wndListCtrl.SetImageList(&m_imageList, LVSIL_SMALL);
124
125 // Setup columns
126 m_wndListCtrl.InsertColumn(0, _T("ID"), LVCFMT_LEFT, 85);
127 m_wndListCtrl.InsertColumn(1, _T("Name"), LVCFMT_LEFT, 100);
128 m_wndListCtrl.InsertColumn(2, _T("Type"), LVCFMT_LEFT, 65);
129 m_wndListCtrl.InsertColumn(3, _T("Full Name"), LVCFMT_LEFT, 170);
130 m_wndListCtrl.InsertColumn(4, _T("Description"), LVCFMT_LEFT, 300);
131 m_wndListCtrl.InsertColumn(5, _T("GUID"), LVCFMT_LEFT, 230);
132
133 LoadListCtrlColumns(m_wndListCtrl, _T("UserEditor"), _T("ListCtrl"));
134
135 PostMessage(WM_COMMAND, ID_VIEW_REFRESH, 0);
136
137 return 0;
138 }
139
140
141 //
142 // Handler for WM_NOTIFY::NM_DBLCLK from IDC_LIST_VIEW
143 //
144
145 void CUserEditor::OnListViewDblClk(LPNMITEMACTIVATE pNMHDR, LRESULT *pResult)
146 {
147 PostMessage(WM_COMMAND, ID_USER_PROPERTIES, 0);
148 *pResult = 0;
149 }
150
151
152 //
153 // WM_SIZE message handler
154 //
155
156 void CUserEditor::OnSize(UINT nType, int cx, int cy)
157 {
158 CMDIChildWnd::OnSize(nType, cx, cy);
159 m_wndListCtrl.SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOZORDER);
160 }
161
162
163 //
164 // Add new item to list
165 //
166
167 int CUserEditor::AddListItem(NXC_USER *pUser)
168 {
169 int iItem;
170 TCHAR buffer[256];
171
172 _sntprintf(buffer, 256, _T("%08X"), pUser->dwId);
173 iItem = m_wndListCtrl.InsertItem(0x7FFFFFFF, buffer,
174 (pUser->dwId == GROUP_EVERYONE) ? 2 : ((pUser->dwId & GROUP_FLAG) ? 1 : 0));
175 if (iItem != -1)
176 {
177 m_wndListCtrl.SetItemData(iItem, pUser->dwId);
178 m_wndListCtrl.SetItemText(iItem, 1, pUser->szName);
179 m_wndListCtrl.SetItemText(iItem, 2, (pUser->dwId & GROUP_FLAG) ? _T("Group") : _T("User"));
180 m_wndListCtrl.SetItemText(iItem, 3, pUser->szFullName);
181 m_wndListCtrl.SetItemText(iItem, 4, pUser->szDescription);
182 m_wndListCtrl.SetItemText(iItem, 5, uuid_to_string(pUser->guid, buffer));
183 m_wndListCtrl.SetItemState(iItem, INDEXTOOVERLAYMASK(pUser->wFlags & UF_DISABLED ? 1 : 0), LVIS_OVERLAYMASK);
184 }
185 return iItem;
186 }
187
188
189 //
190 // WM_COMMAND::ID_VIEW_REFRESH message handler
191 //
192
193 void CUserEditor::OnViewRefresh()
194 {
195 NXC_USER *pUserList;
196 DWORD i, dwNumUsers;
197
198 m_wndListCtrl.DeleteAllItems();
199
200 // Fill in list view
201 if (NXCGetUserDB(g_hSession, &pUserList, &dwNumUsers))
202 {
203 for(i = 0; i < dwNumUsers; i++)
204 if (!(pUserList[i].wFlags & UF_DELETED))
205 AddListItem(&pUserList[i]);
206 SortList();
207 }
208 }
209
210
211 //
212 // WM_COMMAND::ID_USER_CREATE_USER message handler
213 //
214
215 void CUserEditor::OnUserCreateUser()
216 {
217 CNewUserDlg wndDialog;
218
219 wndDialog.m_strTitle = _T("New User");
220 wndDialog.m_strHeader = _T("Login name");
221 wndDialog.m_bDefineProperties = TRUE;
222 if (wndDialog.DoModal() == IDOK)
223 CreateUserObject((LPCTSTR)wndDialog.m_strName, FALSE, wndDialog.m_bDefineProperties);
224 }
225
226
227 //
228 // WM_COMMAND::ID_USER_CREATE_GROUP message handler
229 //
230
231 void CUserEditor::OnUserCreateGroup()
232 {
233 CNewUserDlg wndDialog;
234
235 wndDialog.m_strTitle = _T("New Group");
236 wndDialog.m_strHeader = _T("Group name");
237 wndDialog.m_bDefineProperties = TRUE;
238 if (wndDialog.DoModal() == IDOK)
239 CreateUserObject((LPCTSTR)wndDialog.m_strName, TRUE, wndDialog.m_bDefineProperties);
240 }
241
242
243 //
244 // Create user or group
245 //
246
247 void CUserEditor::CreateUserObject(const TCHAR *pszName, BOOL bIsGroup, BOOL bShowProp)
248 {
249 DWORD dwResult, dwNewId;
250
251 // Send request to server
252 dwResult = DoRequestArg4(NXCCreateUser, g_hSession, (void *)pszName, (void *)bIsGroup,
253 &dwNewId, bIsGroup ? _T("Creating new group...") : _T("Creating new user..."));
254 if (dwResult == RCC_SUCCESS)
255 {
256 int iItem, iOldItem;
257 LVFINDINFO lvfi;
258
259 // Find related item
260 lvfi.flags = LVFI_PARAM;
261 lvfi.lParam = dwNewId;
262 iItem = m_wndListCtrl.FindItem(&lvfi, -1);
263
264 if (iItem == -1)
265 {
266 NXC_USER user;
267
268 memset(&user, 0, sizeof(NXC_USER));
269 user.dwId = dwNewId;
270 nx_strncpy(user.szName, pszName, MAX_USER_NAME);
271 iItem = AddListItem(&user);
272 }
273
274 // Select newly created item
275 iOldItem = m_wndListCtrl.GetNextItem(-1, LVNI_FOCUSED);
276 if (iOldItem != -1)
277 m_wndListCtrl.SetItemState(iOldItem, 0, LVIS_SELECTED | LVIS_FOCUSED);
278 m_wndListCtrl.EnsureVisible(iItem, FALSE);
279 m_wndListCtrl.SetItemState(iItem, LVIS_SELECTED | LVIS_FOCUSED,
280 LVIS_SELECTED | LVIS_FOCUSED);
281
282 SortList();
283
284 if (bShowProp)
285 PostMessage(WM_COMMAND, ID_USER_PROPERTIES, 0);
286 }
287 else
288 {
289 TCHAR szBuffer[256];
290
291 _stprintf(szBuffer, _T("Error creating %s object: %s"),
292 bIsGroup ? _T("group") : _T("user"), NXCGetErrorText(dwResult));
293 MessageBox(szBuffer, _T("Error"), MB_OK | MB_ICONSTOP);
294 }
295 }
296
297
298 //
299 // WM_USERDB_CHANGE message handler
300 //
301
302 void CUserEditor::OnUserDBChange(int iCode, NXC_USER *pUserInfo)
303 {
304 int iItem;
305 LVFINDINFO lvfi;
306
307 // Find related item
308 lvfi.flags = LVFI_PARAM;
309 lvfi.lParam = pUserInfo->dwId;
310 iItem = m_wndListCtrl.FindItem(&lvfi, -1);
311
312 // Modify list control
313 switch(iCode)
314 {
315 case USER_DB_CREATE:
316 if (iItem == -1)
317 AddListItem(pUserInfo);
318 break;
319 case USER_DB_MODIFY:
320 if (iItem == -1)
321 {
322 AddListItem(pUserInfo);
323 }
324 else
325 {
326 TCHAR buffer[64];
327
328 m_wndListCtrl.SetItemText(iItem, 1, pUserInfo->szName);
329 m_wndListCtrl.SetItemText(iItem, 2, (pUserInfo->dwId & GROUP_FLAG) ? _T("Group") : _T("User"));
330 m_wndListCtrl.SetItemText(iItem, 3, pUserInfo->szFullName);
331 m_wndListCtrl.SetItemText(iItem, 4, pUserInfo->szDescription);
332 m_wndListCtrl.SetItemText(iItem, 5, uuid_to_string(pUserInfo->guid, buffer));
333 m_wndListCtrl.SetItemState(iItem, INDEXTOOVERLAYMASK(pUserInfo->wFlags & UF_DISABLED ? 1 : 0), LVIS_OVERLAYMASK);
334 }
335 break;
336 case USER_DB_DELETE:
337 if (iItem != -1)
338 m_wndListCtrl.DeleteItem(iItem);
339 break;
340 }
341 }
342
343
344 //
345 // WM_SETFOCUS message handler
346 //
347
348 void CUserEditor::OnSetFocus(CWnd* pOldWnd)
349 {
350 CMDIChildWnd::OnSetFocus(pOldWnd);
351 m_wndListCtrl.SetFocus();
352 }
353
354
355 //
356 // WM_COMMAND::ID_USER_PROPERTIES message handler
357 //
358
359 void CUserEditor::OnUserProperties()
360 {
361 int iItem;
362
363 iItem = m_wndListCtrl.GetNextItem(-1, LVNI_FOCUSED);
364 if (iItem != -1)
365 {
366 DWORD dwId;
367 NXC_USER *pUser;
368
369 dwId = m_wndListCtrl.GetItemData(iItem);
370 pUser = NXCFindUserById(g_hSession, dwId);
371 if (pUser != NULL)
372 {
373 NXC_USER userInfo;
374 BOOL bModify = FALSE;
375
376 memset(&userInfo, 0, sizeof(NXC_USER));
377 if (dwId & GROUP_FLAG)
378 {
379 CGroupPropDlg dlg;
380
381 dlg.m_pGroup = pUser;
382 dlg.m_strName = pUser->szName;
383 dlg.m_strDescription = pUser->szDescription;
384 dlg.m_bDisabled = (pUser->wFlags & UF_DISABLED) ? TRUE : FALSE;
385 dlg.m_bDropConn = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_SESSIONS) ? TRUE : FALSE;
386 dlg.m_bManageConfig = (pUser->dwSystemRights & SYSTEM_ACCESS_SERVER_CONFIG) ? TRUE : FALSE;
387 dlg.m_bConfigureTraps = (pUser->dwSystemRights & SYSTEM_ACCESS_CONFIGURE_TRAPS) ? TRUE : FALSE;
388 dlg.m_bEditEventDB = (pUser->dwSystemRights & SYSTEM_ACCESS_EDIT_EVENT_DB) ? TRUE : FALSE;
389 dlg.m_bViewEventDB = (pUser->dwSystemRights & SYSTEM_ACCESS_VIEW_EVENT_DB) ? TRUE : FALSE;
390 dlg.m_bManageUsers = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_USERS) ? TRUE : FALSE;
391 dlg.m_bManageActions = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_ACTIONS) ? TRUE : FALSE;
392 dlg.m_bManageEPP = (pUser->dwSystemRights & SYSTEM_ACCESS_EPP) ? TRUE : FALSE;
393 dlg.m_bDeleteAlarms = (pUser->dwSystemRights & SYSTEM_ACCESS_DELETE_ALARMS) ? TRUE : FALSE;
394 dlg.m_bManagePkg = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_PACKAGES) ? TRUE : FALSE;
395 dlg.m_bManageTools = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_TOOLS) ? TRUE : FALSE;
396 dlg.m_bManageScripts = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_SCRIPTS) ? TRUE : FALSE;
397 dlg.m_bViewTrapLog = (pUser->dwSystemRights & SYSTEM_ACCESS_VIEW_TRAP_LOG) ? TRUE : FALSE;
398 dlg.m_bManageAgentCfg = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_AGENT_CFG) ? TRUE : FALSE;
399 dlg.m_bManageModules = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_MODULES) ? TRUE : FALSE;
400 if (dlg.DoModal() == IDOK)
401 {
402 userInfo.dwId = pUser->dwId;
403 nx_strncpy(userInfo.szName, (LPCTSTR)dlg.m_strName, MAX_USER_NAME);
404 nx_strncpy(userInfo.szDescription, (LPCTSTR)dlg.m_strDescription, MAX_USER_DESCR);
405 userInfo.wFlags = dlg.m_bDisabled ? UF_DISABLED : 0;
406 userInfo.dwSystemRights = (dlg.m_bDropConn ? SYSTEM_ACCESS_MANAGE_SESSIONS : 0) |
407 (dlg.m_bManageUsers ? SYSTEM_ACCESS_MANAGE_USERS : 0) |
408 (dlg.m_bManageActions ? SYSTEM_ACCESS_MANAGE_ACTIONS : 0) |
409 (dlg.m_bManageEPP ? SYSTEM_ACCESS_EPP : 0) |
410 (dlg.m_bManageConfig ? SYSTEM_ACCESS_SERVER_CONFIG : 0) |
411 (dlg.m_bConfigureTraps ? SYSTEM_ACCESS_CONFIGURE_TRAPS : 0) |
412 (dlg.m_bEditEventDB ? SYSTEM_ACCESS_EDIT_EVENT_DB : 0) |
413 (dlg.m_bViewEventDB ? SYSTEM_ACCESS_VIEW_EVENT_DB : 0) |
414 (dlg.m_bDeleteAlarms ? SYSTEM_ACCESS_DELETE_ALARMS : 0) |
415 (dlg.m_bManagePkg ? SYSTEM_ACCESS_MANAGE_PACKAGES : 0) |
416 (dlg.m_bManageTools ? SYSTEM_ACCESS_MANAGE_TOOLS : 0) |
417 (dlg.m_bManageScripts ? SYSTEM_ACCESS_MANAGE_SCRIPTS : 0) |
418 (dlg.m_bViewTrapLog ? SYSTEM_ACCESS_VIEW_TRAP_LOG : 0) |
419 (dlg.m_bManageAgentCfg ? SYSTEM_ACCESS_MANAGE_AGENT_CFG : 0) |
420 (dlg.m_bManageModules ? SYSTEM_ACCESS_MANAGE_MODULES : 0);
421 userInfo.dwNumMembers = dlg.m_dwNumMembers;
422 if (userInfo.dwNumMembers > 0)
423 {
424 userInfo.pdwMemberList = (DWORD *)malloc(sizeof(DWORD) * userInfo.dwNumMembers);
425 memcpy(userInfo.pdwMemberList, dlg.m_pdwMembers, sizeof(DWORD) * userInfo.dwNumMembers);
426 }
427 bModify = TRUE;
428 }
429 }
430 else
431 {
432 CUserPropDlg dlg;
433
434 dlg.m_pUser = pUser;
435 dlg.m_strLogin = pUser->szName;
436 dlg.m_strFullName = pUser->szFullName;
437 dlg.m_strDescription = pUser->szDescription;
438 dlg.m_nAuthMethod = pUser->nAuthMethod;
439 dlg.m_nMappingMethod = pUser->nCertMappingMethod;
440 dlg.m_strMappingData = CHECK_NULL_EX(pUser->pszCertMappingData);
441 dlg.m_bAccountDisabled = (pUser->wFlags & UF_DISABLED) ? TRUE : FALSE;
442 dlg.m_bChangePassword = (pUser->wFlags & UF_CHANGE_PASSWORD) ? TRUE : FALSE;
443 dlg.m_bDropConn = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_SESSIONS) ? TRUE : FALSE;
444 dlg.m_bManageConfig = (pUser->dwSystemRights & SYSTEM_ACCESS_SERVER_CONFIG) ? TRUE : FALSE;
445 dlg.m_bConfigureTraps = (pUser->dwSystemRights & SYSTEM_ACCESS_CONFIGURE_TRAPS) ? TRUE : FALSE;
446 dlg.m_bEditEventDB = (pUser->dwSystemRights & SYSTEM_ACCESS_EDIT_EVENT_DB) ? TRUE : FALSE;
447 dlg.m_bViewEventDB = (pUser->dwSystemRights & SYSTEM_ACCESS_VIEW_EVENT_DB) ? TRUE : FALSE;
448 dlg.m_bManageUsers = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_USERS) ? TRUE : FALSE;
449 dlg.m_bManageActions = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_ACTIONS) ? TRUE : FALSE;
450 dlg.m_bManageEPP = (pUser->dwSystemRights & SYSTEM_ACCESS_EPP) ? TRUE : FALSE;
451 dlg.m_bDeleteAlarms = (pUser->dwSystemRights & SYSTEM_ACCESS_DELETE_ALARMS) ? TRUE : FALSE;
452 dlg.m_bManagePkg = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_PACKAGES) ? TRUE : FALSE;
453 dlg.m_bManageTools = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_TOOLS) ? TRUE : FALSE;
454 dlg.m_bManageScripts = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_SCRIPTS) ? TRUE : FALSE;
455 dlg.m_bViewTrapLog = (pUser->dwSystemRights & SYSTEM_ACCESS_VIEW_TRAP_LOG) ? TRUE : FALSE;
456 dlg.m_bManageAgentCfg = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_AGENT_CFG) ? TRUE : FALSE;
457 dlg.m_bManageModules = (pUser->dwSystemRights & SYSTEM_ACCESS_MANAGE_MODULES) ? TRUE : FALSE;
458 if (dlg.DoModal() == IDOK)
459 {
460 userInfo.dwId = pUser->dwId;
461 nx_strncpy(userInfo.szName, (LPCTSTR)dlg.m_strLogin, MAX_USER_NAME);
462 nx_strncpy(userInfo.szFullName, (LPCTSTR)dlg.m_strFullName, MAX_USER_FULLNAME);
463 nx_strncpy(userInfo.szDescription, (LPCTSTR)dlg.m_strDescription, MAX_USER_DESCR);
464 userInfo.nAuthMethod = dlg.m_nAuthMethod;
465 userInfo.nCertMappingMethod = dlg.m_nMappingMethod;
466 userInfo.pszCertMappingData = _tcsdup((LPCTSTR)dlg.m_strMappingData);
467 userInfo.wFlags = (dlg.m_bAccountDisabled ? UF_DISABLED : 0) |
468 (dlg.m_bChangePassword ? UF_CHANGE_PASSWORD : 0);
469 userInfo.dwSystemRights = (dlg.m_bDropConn ? SYSTEM_ACCESS_MANAGE_SESSIONS : 0) |
470 (dlg.m_bManageUsers ? SYSTEM_ACCESS_MANAGE_USERS : 0) |
471 (dlg.m_bManageActions ? SYSTEM_ACCESS_MANAGE_ACTIONS : 0) |
472 (dlg.m_bManageEPP ? SYSTEM_ACCESS_EPP : 0) |
473 (dlg.m_bManageConfig ? SYSTEM_ACCESS_SERVER_CONFIG : 0) |
474 (dlg.m_bConfigureTraps ? SYSTEM_ACCESS_CONFIGURE_TRAPS : 0) |
475 (dlg.m_bEditEventDB ? SYSTEM_ACCESS_EDIT_EVENT_DB : 0) |
476 (dlg.m_bViewEventDB ? SYSTEM_ACCESS_VIEW_EVENT_DB : 0) |
477 (dlg.m_bDeleteAlarms ? SYSTEM_ACCESS_DELETE_ALARMS : 0) |
478 (dlg.m_bManagePkg ? SYSTEM_ACCESS_MANAGE_PACKAGES : 0) |
479 (dlg.m_bManageTools ? SYSTEM_ACCESS_MANAGE_TOOLS : 0) |
480 (dlg.m_bManageScripts ? SYSTEM_ACCESS_MANAGE_SCRIPTS : 0) |
481 (dlg.m_bViewTrapLog ? SYSTEM_ACCESS_VIEW_TRAP_LOG : 0) |
482 (dlg.m_bManageAgentCfg ? SYSTEM_ACCESS_MANAGE_AGENT_CFG : 0) |
483 (dlg.m_bManageModules ? SYSTEM_ACCESS_MANAGE_MODULES : 0);
484 bModify = TRUE;
485 }
486 }
487
488 // Modify user if OK was pressed
489 if (bModify)
490 {
491 DWORD dwResult;
492
493 dwResult = DoRequestArg2(NXCModifyUser, g_hSession, &userInfo, _T("Updating user database..."));
494 if (dwResult != RCC_SUCCESS)
495 theApp.ErrorBox(dwResult, _T("Cannot modify user record: %s"));
496
497 // Cleanup
498 safe_free(userInfo.pdwMemberList);
499 safe_free(userInfo.pszCertMappingData);
500 }
501 }
502 else
503 {
504 MessageBox(_T("Attempt to edit non-existing user record"), _T("Internal Error"), MB_ICONSTOP | MB_OK);
505 PostMessage(WM_COMMAND, ID_VIEW_REFRESH, 0);
506 }
507 }
508 }
509
510
511 //
512 // WM_COMMAND::ID_USER_DELETE message handler
513 //
514
515 void CUserEditor::OnUserDelete()
516 {
517 int iItem;
518
519 iItem = m_wndListCtrl.GetNextItem(-1, LVNI_FOCUSED);
520 if (iItem != -1)
521 {
522 DWORD dwId;
523
524 dwId = m_wndListCtrl.GetItemData(iItem);
525 if (dwId == 0)
526 {
527 MessageBox(_T("System administrator account cannot be deleted"),
528 _T("Warning"), MB_ICONEXCLAMATION | MB_OK);
529 }
530 else if (dwId == GROUP_EVERYONE)
531 {
532 MessageBox(_T("Everyone group cannot be deleted"),
533 _T("Warning"), MB_ICONEXCLAMATION | MB_OK);
534 }
535 else
536 {
537 NXC_USER *pUser;
538
539 pUser = NXCFindUserById(g_hSession, dwId);
540 if (pUser != NULL)
541 {
542 TCHAR szBuffer[256];
543
544 _stprintf(szBuffer, _T("Do you really wish to delete %s %s ?"),
545 dwId & GROUP_FLAG ? _T("group") : _T("user"), pUser->szName);
546 if (MessageBox(szBuffer, _T("Delete account"), MB_ICONQUESTION | MB_YESNO) == IDYES)
547 {
548 DWORD dwResult;
549
550 dwResult = DoRequestArg2(NXCDeleteUser, g_hSession, (void *)dwId, _T("Deleting user..."));
551 if (dwResult != RCC_SUCCESS)
552 theApp.ErrorBox(dwResult, _T("Cannot delete user record: %s"));
553 }
554 }
555 else
556 {
557 MessageBox(_T("Attempt to delete non-existing user record"), _T("Internal Error"), MB_ICONSTOP | MB_OK);
558 PostMessage(WM_COMMAND, ID_VIEW_REFRESH, 0);
559 }
560 }
561 }
562 }
563
564
565 //
566 // WM_CONTEXTMENU message handler
567 //
568
569 void CUserEditor::OnContextMenu(CWnd* pWnd, CPoint point)
570 {
571 CMenu *pMenu;
572
573 pMenu = theApp.GetContextMenu(0);
574 pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
575 }
576
577
578 //
579 // WM_NOTIFY::LVN_ITEMACTIVATE message handler
580 //
581
582 void CUserEditor::OnListViewItemChange(LPNMLISTVIEW pNMHDR, LRESULT *pResult)
583 {
584 if (pNMHDR->iItem != -1)
585 if (pNMHDR->uChanged & LVIF_STATE)
586 {
587 if (pNMHDR->uNewState & LVIS_FOCUSED)
588 {
589 m_dwCurrentUser = m_wndListCtrl.GetItemData(pNMHDR->iItem);
590 }
591 else
592 {
593 m_dwCurrentUser = INVALID_UID;
594 }
595 }
596 }
597
598
599 //
600 // WM_COMMAND::ID_USER_SETPASSWORD message handler
601 //
602
603 void CUserEditor::OnUserSetpassword()
604 {
605 int iItem;
606
607 iItem = m_wndListCtrl.GetNextItem(-1, LVNI_FOCUSED);
608 if (iItem != -1)
609 {
610 DWORD dwId;
611 NXC_USER *pUser;
612
613 dwId = m_wndListCtrl.GetItemData(iItem);
614
615 pUser = NXCFindUserById(g_hSession, dwId);
616 if (pUser != NULL)
617 {
618 CPasswordChangeDlg dlg(IDD_SET_PASSWORD);
619
620 if (dlg.DoModal() == IDOK)
621 {
622 DWORD dwResult;
623
624 dwResult = DoRequestArg3(NXCSetPassword, g_hSession, (void *)dwId, dlg.m_szPassword, _T("Changing password..."));
625 if (dwResult != RCC_SUCCESS)
626 theApp.ErrorBox(dwResult, _T("Cannot change password: %s"));
627 else
628 MessageBox(_T("Password was successfully changed"), _T("Information"), MB_ICONINFORMATION | MB_OK);
629 }
630 }
631 else
632 {
633 MessageBox(_T("Attempt to modify non-existing user record"), _T("Internal Error"), MB_ICONSTOP | MB_OK);
634 PostMessage(WM_COMMAND, ID_VIEW_REFRESH, 0);
635 }
636 }
637 }
638
639
640 //
641 // UPDATE_COMMAND_UI handlers
642 //
643
644 void CUserEditor::OnUpdateUserProperties(CCmdUI* pCmdUI)
645 {
646 pCmdUI->Enable(m_dwCurrentUser != INVALID_UID);
647 }
648
649 void CUserEditor::OnUpdateUserDelete(CCmdUI* pCmdUI)
650 {
651 pCmdUI->Enable(m_dwCurrentUser != INVALID_UID);
652 }
653
654 void CUserEditor::OnUpdateUserSetpassword(CCmdUI* pCmdUI)
655 {
656 pCmdUI->Enable((m_dwCurrentUser != INVALID_UID) && (!(m_dwCurrentUser & GROUP_FLAG)));
657 }
658
659
660 //
661 // User comparision procedure for sorting
662 //
663
664 static int CALLBACK UserCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
665 {
666 CUserEditor *pWnd = (CUserEditor *)lParamSort;
667 NXC_USER *pUser1, *pUser2;
668 TCHAR guid1[64], guid2[64];
669 int iResult;
670
671 pUser1 = NXCFindUserById(g_hSession, lParam1);
672 pUser2 = NXCFindUserById(g_hSession, lParam2);
673
674 if ((pUser1 == NULL) || (pUser2 == NULL))
675 {
676 theApp.DebugPrintf(_T("WARNING: pUser == NULL in UserCompareProc() !!!"));
677 return 0;
678 }
679
680 switch(pWnd->GetSortMode())
681 {
682 case 0: // ID
683 iResult = COMPARE_NUMBERS(pUser1->dwId, pUser2->dwId);
684 break;
685 case 1: // Name
686 iResult = _tcsicmp(pUser1->szName, pUser2->szName);
687 break;
688 case 2: // Type
689 iResult = COMPARE_NUMBERS(pUser1->dwId & GROUP_FLAG, pUser2->dwId & GROUP_FLAG);
690 break;
691 case 3: // Full name
692 iResult = _tcsicmp(pUser1->szFullName, pUser2->szFullName);
693 break;
694 case 4: // Description
695 iResult = _tcsicmp(pUser1->szDescription, pUser2->szDescription);
696 break;
697 case 5: // GUID
698 iResult = _tcsicmp(uuid_to_string(pUser1->guid, guid1), uuid_to_string(pUser2->guid, guid2));
699 break;
700 default:
701 iResult = 0;
702 break;
703 }
704
705 return iResult * pWnd->GetSortDir();
706 }
707
708
709 //
710 // Sort user list
711 //
712
713 void CUserEditor::SortList()
714 {
715 LVCOLUMN lvc;
716
717 m_wndListCtrl.SortItems(UserCompareProc, (LPARAM)this);
718 lvc.mask = LVCF_IMAGE | LVCF_FMT;
719 lvc.fmt = LVCFMT_LEFT | LVCFMT_IMAGE | LVCFMT_BITMAP_ON_RIGHT;
720 lvc.iImage = (m_iSortDir == 1) ? m_iSortImageBase : m_iSortImageBase + 1;
721 m_wndListCtrl.SetColumn(m_iSortMode, &lvc);
722 }
723
724
725 //
726 // WM_NOTIFY::LVN_COLUMNCLICK message handler
727 //
728
729 void CUserEditor::OnListViewColumnClick(LPNMLISTVIEW pNMHDR, LRESULT *pResult)
730 {
731 LVCOLUMN lvCol;
732
733 // Unmark old sorting column
734 lvCol.mask = LVCF_FMT;
735 lvCol.fmt = LVCFMT_LEFT;
736 m_wndListCtrl.SetColumn(m_iSortMode, &lvCol);
737
738 // Change current sort mode and resort list
739 if (m_iSortMode == pNMHDR->iSubItem)
740 {
741 // Same column, change sort direction
742 m_iSortDir = -m_iSortDir;
743 }
744 else
745 {
746 // Another sorting column
747 m_iSortMode = pNMHDR->iSubItem;
748 }
749 SortList();
750
751 *pResult = 0;
752 }