File: role_tree_model.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (367 lines) | stat: -rw-r--r-- 7,595 bytes parent folder | download | duplicates (2)
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
/* 
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "role_tree_model.h"

#include "base/string_utilities.h"

#include "grtpp_undo_manager.h"

using namespace bec;
using namespace base;

RoleTreeBE::RoleTreeBE(const db_CatalogRef &catalog)
: _catalog(catalog), _root(0)
{
  refresh();
}


RoleTreeBE::~RoleTreeBE()
{
  delete _root;
}

void RoleTreeBE::set_object(const db_DatabaseObjectRef &object)
{
  _object_id= object.id();
}


void RoleTreeBE::add_role_children_to_node(Node *parent_node)
{
  if(!parent_node->role->childRoles().is_valid())
    return;

  grt::ListRef<db_Role> roles(parent_node->role->childRoles());

  for (size_t c= roles.count(), i= 0; i < c; i++)
  {
    Node *n= new Node;
    n->role= roles.get(i);
    n->parent= parent_node;

    parent_node->children.push_back(n);
    add_role_children_to_node(n);
  }
}


void RoleTreeBE::refresh()
{
/*
  //delete _root;

  _root= new Node;

  grt::ListRef<db_Role> roles(_catalog.roles());
  _root->role= db_Role(_catalog.grt());
  _root->role.childRoles(grt::ListRef<db_Role>(_catalog.grt()));

  for (size_t c= roles.count(), i= 0; i < c; i++)
  {
    //if (!roles[i]->parentRole().is_valid()) // top-level role
    {
      Node *n= new Node;
      n->role= roles[i];
      n->parent= _root;

      _root->children.push_back(n);
      _root->role.childRoles().insert(n->role, -1);
      add_role_children_to_node(n);
    }
  }
*/

  std::map<std::string, Node*> nodes;

  delete _root;

  _root= new Node;

  grt::ListRef<db_Role> roles(_catalog->roles());

  for (size_t c= roles.count(), i= 0; i < c; i++)
  {
    Node *n= new Node;
    n->role= roles[i];
  
    nodes[n->role.id()]= n;
  }

  for (size_t c= roles.count(), i= 0; i < c; i++)
  {
    if (roles[i]->parentRole().is_valid())
    {
      if (nodes.find(roles[i]->parentRole().id()) == nodes.end())
      {
        Node *next_node= nodes[roles[i]->id()];
        _root->children.push_back(next_node);
        next_node->parent= _root;
      }
      else
      {
        Node *parent= nodes[roles[i]->parentRole().id()];
        Node *next_node= nodes[roles[i]->id()];
        parent->children.push_back(next_node);
        next_node->parent= parent;
      }
    }
    else
    {
      Node *next_node= nodes[roles[i]->id()];
      _root->children.push_back(nodes[roles[i]->id()]);
      next_node->parent= _root;
    }
  }
}


size_t RoleTreeBE::count_children(const NodeId &parent)
{
  Node *node= get_node_with_id(parent);

  if (node)
    return (int)node->children.size();

  return 0;
}


NodeId RoleTreeBE::get_child(const NodeId &parent, size_t index)
{
  Node *n= get_node_with_id(parent);
  if (n && index < n->children.size())
    return NodeId(parent).append(index);
  if (n)
    throw std::logic_error("invalid index");

  return NodeId();
}


bool RoleTreeBE::set_field(const NodeId &node, ColumnId column, const std::string &value)
{
  Node *n;

  switch ((Columns)column)
  {
  case Enabled:
    return false;

  case Name:
    n= get_node_with_id(node);
    if (n)
    {
      // Switched to AutoUndoEdit allow for Live editors to not track undo, but
      // RoleTree is not used by live editors, so it's ok to keep AutoUndo here.
      grt::AutoUndo undo(_catalog.get_grt());
  //    AutoUndoEdit undo(this);

      n->role->name(value);
      
      undo.end(strfmt(_("Rename Role to '%s'"), value.c_str()));
      return true;
    }
    return false;
  }
  return false;
}


grt::Type RoleTreeBE::get_field_type(const NodeId &node, ColumnId column)
{
  switch ((Columns)column)
  {
  case Enabled:
    return grt::IntegerType;
  case Name:
    return grt::StringType;
  }
  throw std::logic_error("Invalid column");
}


bool RoleTreeBE::get_field_grt(const NodeId &node, ColumnId column, grt::ValueRef &value)
{
  Node *n;

  switch ((Columns)column)
  {
  case Enabled:
    n= get_node_with_id(node);
    if (n)
    {
      if (_object_id.empty())  
      { // returns whether there is anything in the privs list
        value= grt::IntegerRef(n->role->privileges().count() > 0 ? 1 : 0);
      }
      else
      {
        // returns whether the object is in the privs list of the role
        bool flag= false;
        for (size_t c= n->role->privileges().count(), i= 0; i < c; i++)
        {
          db_DatabaseObjectRef obj(n->role->privileges().get(i)->databaseObject());
          if (obj.is_valid() && obj.id() == _object_id)
          {
            flag= true;
            break;
          }
        }
        value= grt::IntegerRef(flag ? 1 : 0);
      }
      return true;
    }
    return false;

  case Name:
    n= get_node_with_id(node);
    if (n)
    {
      value= n->role->name();
      return true;
    }
    return false;
  }

  return false;
}



RoleTreeBE::Node *RoleTreeBE::get_node_with_id(const NodeId &node)
{
  Node *n= _root;

  if (!n)
    return 0;

  if (node.depth() == 0)
    return n;

  for (size_t i= 0; i < node.depth(); i++)
  {
    if (node[i] >= n->children.size())
      throw std::logic_error("Invalid node id");

    n= n->children[node[i]];
  }
  return n;
}


bool RoleTreeBE::find_role(const RoleTreeBE::Node *node, const db_RoleRef &role, bec::NodeId &path)
{
  int i= 0;

  if (node->role == role)
    return true;

  for (std::vector<RoleTreeBE::Node*>::const_iterator r= node->children.begin();
	r != node->children.end(); ++r, i++)
  {
    if (find_role(*r, role, path))
    {
      path.prepend(i);
      return true;
    }
  }
  return false;
}


NodeId RoleTreeBE::node_id_for_role(const db_RoleRef &role)
{
  NodeId node;
  if (find_role(_root, role, node))
    return node;
  return NodeId();
}


void RoleTreeBE::erase_node(const NodeId &node)
{
  Node *n= get_node_with_id(node);
  if(!n)
    return;
  
  Node *parent= n->parent;
  if(!parent)
    return;

  parent->erase_child(n);
}

bool RoleTreeBE::is_parent_child(Node *parent, Node *child)
{
  for(; child; child= child->parent)
  {
    if(child->parent == parent)
      return true;
  }
  return false;
}

void RoleTreeBE::append_child(const NodeId &parent, const NodeId &child)
{
  Node *p= get_node_with_id(parent);
  Node *c= get_node_with_id(child);
  
  if(!p || !c || is_parent_child(c, p))
    return;

  erase_node(child);
  
  p->append_child(c);
}

void RoleTreeBE::insert_node_after(const NodeId &after, const NodeId &node)
{
  Node *n= get_node_with_id(node);
  Node *a= get_node_with_id(after);
  
  if(!n || !a)
    return;

  erase_node(node);
  
  a->parent->insert_child_after(a, n);
}

void RoleTreeBE::insert_node_before(const NodeId &before, const NodeId &node)
{
  Node *n= get_node_with_id(node);
  Node *b= get_node_with_id(before);

  if(!n || !b)
    return;

  erase_node(node);
  
  b->parent->insert_child_before(b, n);
}

void RoleTreeBE::move_to_top_level(const NodeId &node)
{
  Node *n= get_node_with_id(node);
  if(n != NULL)
    n->role->parentRole(db_RoleRef());
}