- Finished migration to new client library model
[public/netxms.git] / src / console / win32 / ObjectPropsSecurity.cpp
1 // ObjectPropsSecurity.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "nxcon.h"
6 #include "ObjectPropsSecurity.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 // CObjectPropsSecurity property page
16
17 IMPLEMENT_DYNCREATE(CObjectPropsSecurity, CPropertyPage)
18
19 CObjectPropsSecurity::CObjectPropsSecurity() : CPropertyPage(CObjectPropsSecurity::IDD)
20 {
21 //{{AFX_DATA_INIT(CObjectPropsSecurity)
22 m_bInheritRights = FALSE;
23 //}}AFX_DATA_INIT
24 m_pAccessList = NULL;
25 m_dwAclSize = 0;
26 }
27
28 CObjectPropsSecurity::~CObjectPropsSecurity()
29 {
30 safe_free(m_pAccessList);
31 }
32
33 void CObjectPropsSecurity::DoDataExchange(CDataExchange* pDX)
34 {
35 CPropertyPage::DoDataExchange(pDX);
36 //{{AFX_DATA_MAP(CObjectPropsSecurity)
37 DDX_Control(pDX, IDC_CHECK_ACK_ALARMS, m_wndCheckAckAlarms);
38 DDX_Control(pDX, IDC_CHECK_VIEW_ALARMS, m_wndCheckViewAlarms);
39 DDX_Control(pDX, IDC_CHECK_ACCESS, m_wndCheckAccess);
40 DDX_Control(pDX, IDC_CHECK_CREATE, m_wndCheckCreate);
41 DDX_Control(pDX, IDC_CHECK_READ, m_wndCheckRead);
42 DDX_Control(pDX, IDC_CHECK_MODIFY, m_wndCheckModify);
43 DDX_Control(pDX, IDC_CHECK_DELETE, m_wndCheckDelete);
44 DDX_Control(pDX, IDC_LIST_USERS, m_wndUserList);
45 DDX_Check(pDX, IDC_CHECK_INHERIT_RIGHTS, m_bInheritRights);
46 //}}AFX_DATA_MAP
47 }
48
49
50 BEGIN_MESSAGE_MAP(CObjectPropsSecurity, CPropertyPage)
51 //{{AFX_MSG_MAP(CObjectPropsSecurity)
52 ON_BN_CLICKED(IDC_ADD_USER, OnAddUser)
53 ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_USERS, OnItemchangedListUsers)
54 ON_BN_CLICKED(IDC_CHECK_INHERIT_RIGHTS, OnCheckInheritRights)
55 ON_BN_CLICKED(IDC_DELETE_USER, OnDeleteUser)
56 ON_BN_CLICKED(IDC_CHECK_READ, OnCheckRead)
57 ON_BN_CLICKED(IDC_CHECK_MODIFY, OnCheckModify)
58 ON_BN_CLICKED(IDC_CHECK_DELETE, OnCheckDelete)
59 ON_BN_CLICKED(IDC_CHECK_CREATE, OnCheckCreate)
60 ON_BN_CLICKED(IDC_CHECK_ACCESS, OnCheckAccess)
61 ON_BN_CLICKED(IDC_CHECK_VIEW_ALARMS, OnCheckViewAlarms)
62 ON_BN_CLICKED(IDC_CHECK_ACK_ALARMS, OnCheckAckAlarms)
63 //}}AFX_MSG_MAP
64 END_MESSAGE_MAP()
65
66 /////////////////////////////////////////////////////////////////////////////
67 // CObjectPropsSecurity message handlers
68
69
70 //
71 // WM_INITDIALOG message handler
72 //
73
74 BOOL CObjectPropsSecurity::OnInitDialog()
75 {
76 DWORD i;
77 NXC_USER *pUser;
78 int iItem;
79 CImageList *pImageList;
80 RECT rect;
81
82 CPropertyPage::OnInitDialog();
83
84 m_pUpdate = ((CObjectPropSheet *)GetParent())->GetUpdateStruct();
85 m_bIsModified = FALSE;
86
87 // Setup list view
88 m_wndUserList.GetClientRect(&rect);
89 m_wndUserList.InsertColumn(0, "Name", LVCFMT_LEFT,
90 rect.right - GetSystemMetrics(SM_CXVSCROLL) - 4);
91 m_wndUserList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_TRACKSELECT | LVS_EX_UNDERLINEHOT);
92 m_wndUserList.SetHoverTime(0x7FFFFFFF);
93
94 // Create image list
95 pImageList = new CImageList;
96 pImageList->Create(16, 16, ILC_COLOR8 | ILC_MASK, 8, 8);
97 pImageList->Add(AfxGetApp()->LoadIcon(IDI_USER));
98 pImageList->Add(AfxGetApp()->LoadIcon(IDI_USER_GROUP));
99 pImageList->Add(AfxGetApp()->LoadIcon(IDI_EVERYONE));
100 m_wndUserList.SetImageList(pImageList, LVSIL_SMALL);
101
102 // Copy object's ACL
103 m_dwAclSize = m_pObject->dwAclSize;
104 m_pAccessList = (NXC_ACL_ENTRY *)nx_memdup(m_pObject->pAccessList,
105 sizeof(NXC_ACL_ENTRY) * m_dwAclSize);
106
107 // Populate user list with current ACL data
108 for(i = 0; i < m_dwAclSize; i++)
109 {
110 pUser = NXCFindUserById(g_hSession, m_pAccessList[i].dwUserId);
111 if (pUser != NULL)
112 {
113 iItem = m_wndUserList.InsertItem(0x7FFFFFFF, pUser->szName,
114 (pUser->dwId == GROUP_EVERYONE) ? 2 :
115 ((pUser->dwId & GROUP_FLAG) ? 1 : 0));
116 if (iItem != -1)
117 m_wndUserList.SetItemData(iItem, m_pAccessList[i].dwUserId);
118 }
119 }
120
121 // We have no selection by default, so disable all checkboxes
122 m_dwCurrAclEntry = INVALID_INDEX;
123 m_wndCheckRead.EnableWindow(FALSE);
124 m_wndCheckModify.EnableWindow(FALSE);
125 m_wndCheckCreate.EnableWindow(FALSE);
126 m_wndCheckDelete.EnableWindow(FALSE);
127 m_wndCheckViewAlarms.EnableWindow(FALSE);
128 m_wndCheckAckAlarms.EnableWindow(FALSE);
129 m_wndCheckAccess.EnableWindow(FALSE);
130
131 return TRUE;
132 }
133
134
135 //
136 // PSN_OK handler
137 //
138
139 void CObjectPropsSecurity::OnOK()
140 {
141 CPropertyPage::OnOK();
142
143 if (m_bIsModified)
144 {
145 m_pUpdate->dwFlags |= OBJ_UPDATE_ACL;
146 m_pUpdate->bInheritRights = m_bInheritRights;
147 m_pUpdate->dwAclSize = m_dwAclSize;
148 m_pUpdate->pAccessList = (NXC_ACL_ENTRY *)nx_memdup(m_pAccessList,
149 sizeof(NXC_ACL_ENTRY) * m_dwAclSize);
150 }
151 }
152
153
154 //
155 // Handler for "Add user..." button
156 //
157
158 void CObjectPropsSecurity::OnAddUser()
159 {
160 CUserSelectDlg wndSelectDlg;
161
162 if (wndSelectDlg.DoModal() == IDOK)
163 {
164 DWORD i;
165 int iItem = -1;
166
167 // Check if we have this user in ACL already
168 for(i = 0; i < m_dwAclSize; i++)
169 if (m_pAccessList[i].dwUserId == wndSelectDlg.m_dwUserId)
170 {
171 LVFINDINFO lvfi;
172
173 // Find appropriate item in list
174 lvfi.flags = LVFI_PARAM;
175 lvfi.lParam = m_pAccessList[i].dwUserId;
176 iItem = m_wndUserList.FindItem(&lvfi);
177 break;
178 }
179
180 if (i == m_dwAclSize)
181 {
182 NXC_USER *pUser;
183
184 // Create new entry in ACL
185 m_pAccessList = (NXC_ACL_ENTRY *)realloc(m_pAccessList, sizeof(NXC_ACL_ENTRY) * (m_dwAclSize + 1));
186 m_pAccessList[m_dwAclSize].dwUserId = wndSelectDlg.m_dwUserId;
187 m_pAccessList[m_dwAclSize].dwAccessRights = OBJECT_ACCESS_READ;
188 m_dwAclSize++;
189
190 // Add new line to user list
191 pUser = NXCFindUserById(g_hSession, wndSelectDlg.m_dwUserId);
192 if (pUser != NULL)
193 {
194 iItem = m_wndUserList.InsertItem(0x7FFFFFFF, pUser->szName,
195 (pUser->dwId == GROUP_EVERYONE) ? 2 :
196 ((pUser->dwId & GROUP_FLAG) ? 1 : 0));
197 m_wndUserList.SetItemData(iItem, wndSelectDlg.m_dwUserId);
198 }
199
200 m_bIsModified = TRUE;
201 }
202
203 // Select new item
204 if (iItem != -1)
205 SelectListViewItem(&m_wndUserList, iItem);
206 }
207 }
208
209
210 //
211 // Handler for "Delete user" button
212 //
213
214 void CObjectPropsSecurity::OnDeleteUser()
215 {
216 // TODO: Add your control notification handler code here
217
218 }
219
220
221 //
222 // LVN_ITEMCHANGED handler for IDC_LIST_USERS
223 //
224
225 void CObjectPropsSecurity::OnItemchangedListUsers(NMHDR* pNMHDR, LRESULT* pResult)
226 {
227 NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
228 NXC_USER *pUser;
229 DWORD i;
230
231 if (pNMListView->iItem != -1)
232 if ((pNMListView->uChanged & LVIF_STATE) && (pNMListView->uNewState & LVIS_FOCUSED))
233 {
234 pUser = NXCFindUserById(g_hSession, m_wndUserList.GetItemData(pNMListView->iItem));
235 if (pUser != NULL) // It should't be NULL
236 {
237 // Find user in ACL
238 for(i = 0; i < m_dwAclSize; i++)
239 if (pUser->dwId == m_pAccessList[i].dwUserId)
240 break;
241
242 if (i != m_dwAclSize) // Again, just a paranoia check
243 {
244 DWORD dwRights = m_pAccessList[i].dwAccessRights;
245
246 // Set checkboxes
247 m_wndCheckRead.SetCheck((dwRights & OBJECT_ACCESS_READ) ? BST_CHECKED : BST_UNCHECKED);
248 m_wndCheckModify.SetCheck((dwRights & OBJECT_ACCESS_MODIFY) ? BST_CHECKED : BST_UNCHECKED);
249 m_wndCheckCreate.SetCheck((dwRights & OBJECT_ACCESS_CREATE) ? BST_CHECKED : BST_UNCHECKED);
250 m_wndCheckDelete.SetCheck((dwRights & OBJECT_ACCESS_DELETE) ? BST_CHECKED : BST_UNCHECKED);
251 m_wndCheckViewAlarms.SetCheck((dwRights & OBJECT_ACCESS_READ_ALARMS) ? BST_CHECKED : BST_UNCHECKED);
252 m_wndCheckAckAlarms.SetCheck((dwRights & OBJECT_ACCESS_ACK_ALARMS) ? BST_CHECKED : BST_UNCHECKED);
253 m_wndCheckAccess.SetCheck((dwRights & OBJECT_ACCESS_CONTROL) ? BST_CHECKED : BST_UNCHECKED);
254
255 // Enable checkboxes if they was disabled
256 if (m_dwCurrAclEntry == INVALID_INDEX)
257 {
258 m_wndCheckRead.EnableWindow(TRUE);
259 m_wndCheckModify.EnableWindow(TRUE);
260 m_wndCheckCreate.EnableWindow(TRUE);
261 m_wndCheckDelete.EnableWindow(TRUE);
262 m_wndCheckViewAlarms.EnableWindow(TRUE);
263 m_wndCheckAckAlarms.EnableWindow(TRUE);
264 m_wndCheckAccess.EnableWindow(TRUE);
265 }
266 m_dwCurrAclEntry = i;
267 }
268 }
269 }
270
271 *pResult = 0;
272 }
273
274
275 //
276 // Handle clicks on checkboxes
277 //
278
279 void CObjectPropsSecurity::OnCheckInheritRights()
280 {
281 m_bIsModified = TRUE;
282 }
283
284 void CObjectPropsSecurity::OnCheckRead()
285 {
286 m_bIsModified = TRUE;
287 if (m_dwCurrAclEntry != INVALID_INDEX) // It shouldn't be
288 {
289 if (m_wndCheckRead.GetCheck() == BST_CHECKED)
290 m_pAccessList[m_dwCurrAclEntry].dwAccessRights |= OBJECT_ACCESS_READ;
291 else
292 m_pAccessList[m_dwCurrAclEntry].dwAccessRights &= ~OBJECT_ACCESS_READ;
293 }
294 }
295
296 void CObjectPropsSecurity::OnCheckModify()
297 {
298 m_bIsModified = TRUE;
299 if (m_dwCurrAclEntry != INVALID_INDEX) // It shouldn't be
300 {
301 if (m_wndCheckModify.GetCheck() == BST_CHECKED)
302 m_pAccessList[m_dwCurrAclEntry].dwAccessRights |= OBJECT_ACCESS_MODIFY;
303 else
304 m_pAccessList[m_dwCurrAclEntry].dwAccessRights &= ~OBJECT_ACCESS_MODIFY;
305 }
306 }
307
308 void CObjectPropsSecurity::OnCheckDelete()
309 {
310 m_bIsModified = TRUE;
311 if (m_dwCurrAclEntry != INVALID_INDEX) // It shouldn't be
312 {
313 if (m_wndCheckDelete.GetCheck() == BST_CHECKED)
314 m_pAccessList[m_dwCurrAclEntry].dwAccessRights |= OBJECT_ACCESS_DELETE;
315 else
316 m_pAccessList[m_dwCurrAclEntry].dwAccessRights &= ~OBJECT_ACCESS_DELETE;
317 }
318 }
319
320 void CObjectPropsSecurity::OnCheckCreate()
321 {
322 m_bIsModified = TRUE;
323 if (m_dwCurrAclEntry != INVALID_INDEX) // It shouldn't be
324 {
325 if (m_wndCheckCreate.GetCheck() == BST_CHECKED)
326 m_pAccessList[m_dwCurrAclEntry].dwAccessRights |= OBJECT_ACCESS_CREATE;
327 else
328 m_pAccessList[m_dwCurrAclEntry].dwAccessRights &= ~OBJECT_ACCESS_CREATE;
329 }
330 }
331
332 void CObjectPropsSecurity::OnCheckAccess()
333 {
334 m_bIsModified = TRUE;
335 if (m_dwCurrAclEntry != INVALID_INDEX) // It shouldn't be
336 {
337 if (m_wndCheckAccess.GetCheck() == BST_CHECKED)
338 m_pAccessList[m_dwCurrAclEntry].dwAccessRights |= OBJECT_ACCESS_CONTROL;
339 else
340 m_pAccessList[m_dwCurrAclEntry].dwAccessRights &= ~OBJECT_ACCESS_CONTROL;
341 }
342 }
343
344 void CObjectPropsSecurity::OnCheckViewAlarms()
345 {
346 m_bIsModified = TRUE;
347 if (m_dwCurrAclEntry != INVALID_INDEX) // It shouldn't be
348 {
349 if (m_wndCheckViewAlarms.GetCheck() == BST_CHECKED)
350 m_pAccessList[m_dwCurrAclEntry].dwAccessRights |= OBJECT_ACCESS_READ_ALARMS;
351 else
352 m_pAccessList[m_dwCurrAclEntry].dwAccessRights &= ~OBJECT_ACCESS_READ_ALARMS;
353 }
354 }
355
356 void CObjectPropsSecurity::OnCheckAckAlarms()
357 {
358 m_bIsModified = TRUE;
359 if (m_dwCurrAclEntry != INVALID_INDEX) // It shouldn't be
360 {
361 if (m_wndCheckAckAlarms.GetCheck() == BST_CHECKED)
362 m_pAccessList[m_dwCurrAclEntry].dwAccessRights |= OBJECT_ACCESS_ACK_ALARMS;
363 else
364 m_pAccessList[m_dwCurrAclEntry].dwAccessRights &= ~OBJECT_ACCESS_ACK_ALARMS;
365 }
366 }