File: metrics.cpp

package info (click to toggle)
postbooks 4.9.5-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 103,120 kB
  • sloc: cpp: 288,269; sh: 607; xml: 214; awk: 104; makefile: 49
file content (250 lines) | stat: -rw-r--r-- 5,843 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
/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
 * It is licensed to you under the Common Public Attribution License
 * version 1.0, the full text of which (including xTuple-specific Exhibits)
 * is available at www.xtuple.com/CPAL.  By using this software, you agree
 * to be bound by its terms.
 */


#include "metrics.h"
#include <QSqlError>
#include <QSqlDatabase>
#include <QSqlDriver>
#include <QVariant>
#include "xsqlquery.h"
#include <QMessageBox>

Parameters::Parameters(QObject * parent)
  : QObject(parent)
{
  _dirty = false;
}

void Parameters::load()
{
  _values.clear();

  XSqlQuery q;
  q.prepare(_readSql);
  q.bindValue(":username", _username);
  q.exec();
  while (q.next())
    _values[q.value("key").toString()] = q.value("value").toString();
  if (q.lastError().type() != QSqlError::NoError) {
    QMessageBox::critical(0, tr("Error loading %1").arg(metaObject()->className()),
                         q.lastError().text());
    return;
  }

  _dirty = false;

  emit loaded();
}

void Parameters::sSetDirty(const QString &note)
{
    if(note == _notifyName)
        _dirty = true;
}

QString Parameters::value(const char *pName)
{
  return value(QString(pName));
}

QString Parameters::value(const QString &pName)
{
  MetricMap::iterator it = _values.find(pName);
  if (it == _values.end())
    return QString::null;
  else
    return it.value();
}

bool Parameters::boolean(const char *pName)
{
  return boolean(QString(pName));
}

bool Parameters::boolean(const QString &pName)
{
  MetricMap::iterator it = _values.find(pName);
  if (it == _values.end())
    return false;
  else if (it.value() == "t")
    return true;

  return false;
}

void Parameters::set(const char *pName, bool pValue)
{
  set(pName, QString(pValue ? "t" : "f"));
}

void Parameters::set(const QString &pName, bool pValue)
{
  set(pName, QString(pValue ? "t" : "f"));
}

void Parameters::set(const char *pName, int pValue)
{
  set(QString(pName), pValue);
}

void Parameters::set(const QString &pName, int pValue)
{
  set(QString(pName), QString::number(pValue));
}

void Parameters::set(const char *pName, const QString &pValue)
{
  set(QString(pName), pValue);
}

void Parameters::set(const QString &pName, const QString &pValue)
{
  MetricMap::iterator it = _values.find(pName);
  if (it != _values.end())
  {
    if (it.value() == pValue)
      return;
    else
      it.value() = pValue;
  }
  else
    _values[pName] = pValue;

  _set(pName, pValue);
}

void Parameters::_set(const QString &pName, QVariant pValue)
{
  XSqlQuery q;
  q.prepare(_setSql);
  q.bindValue(":username", _username);
  q.bindValue(":name", pName);
  q.bindValue(":value", pValue);
  q.exec();

  _dirty = true;
}

QString Parameters::parent(const QString &pValue)
{
  for (MetricMap::iterator it = _values.begin(); it != _values.end(); it++)
    if (it.value() == pValue)
      return it.key();

  return QString::null;
}


Metrics::Metrics()
{
  _notifyName = "metricsUpdated";
  _readSql = "SELECT metric_name AS key, metric_value AS value FROM metric;";
  _setSql  = "SELECT setMetric(:name, :value);";

  load();
}


Preferences::Preferences(const QString &pUsername)
{
  _notifyName = "preferencesUpdated";
  _readSql  = "SELECT usrpref_name AS key, usrpref_value AS value "
              "FROM usrpref "
              "WHERE (usrpref_username=:username);";
  _setSql   = "SELECT setUserPreference(:username, :name, :value);";
  _username = pUsername;

  load();
}

void Preferences::remove(const QString &pPrefName)
{
  XSqlQuery q;
  q.prepare("SELECT deleteUserPreference(:prefname);");
  q.bindValue(":prefname", pPrefName);
  q.exec();

  _dirty = true;
}


Privileges::Privileges()
{
  _notifyName = "usrprivUpdated";
  QString user;
  XSqlQuery userq("SELECT getEffectiveXtUser() AS user;");
  if (userq.lastError().type() != QSqlError::NoError)
    userq.exec("SELECT CURRENT_USER AS user;");
  if (userq.first())
    user = userq.value("user").toString();

  _readSql = QString("SELECT priv_name AS key, TEXT('t') AS value "
             "  FROM usrpriv, priv "
             " WHERE((usrpriv_priv_id=priv_id)"
             "   AND (usrpriv_username='%1')) "
             " UNION "
             "SELECT priv_name AS key, TEXT('t') AS value "
             "  FROM priv, grppriv, usrgrp"
             " WHERE((usrgrp_grp_id=grppriv_grp_id)"
             "   AND (grppriv_priv_id=priv_id)"
             "   AND (usrgrp_username='%1'));").arg(user);

  QSqlDatabase::database().driver()->subscribeToNotification("usrprivUpdated");
  QObject::connect(QSqlDatabase::database().driver(), SIGNAL(notification(const QString&)),
           this, SLOT(sSetDirty(const QString &)));

  load();
}

bool Privileges::check(const QString &pName)
{
    if (pName == "#superuser")
      return isDba();

    if(_dirty)
      load();

    MetricMap::iterator it = _values.find(pName);
    if (it != _values.end())
      return true;

    if (pName.contains(" ")) {
      QStringList privlist = pName.split(' ', QString::SkipEmptyParts);
      foreach (QString priv, privlist) {
        if (check(priv))
          return true;
      }
    }

    if (pName.contains("+")) {
      bool anded = true;
      QStringList privlist = pName.split('+', QString::SkipEmptyParts);
      foreach (QString priv, privlist) {
        anded = anded && check(priv);
      }
      return anded;
    }

 return false;
}

bool Privileges::isDba()
{
  XSqlQuery su("SELECT isDBA() AS issuper;");
  su.exec();
  if (su.first())
    return su.value("issuper").toBool();
  else if (su.lastError().type() != QSqlError::NoError)
    qWarning("SQL error in Privileges::isDba(): %s",
             qPrintable(su.lastError().text()));

  return false;
}