3 #include <QRegularExpression>
56 , _created(QDateTime::currentDateTimeUtc())
63 , _authtoken(authtoken)
73 VFS::LOG( QString(
"Created '%1' session for user '%2' which will expire on %3").arg(
_type).arg(
_user).arg(
_expires.toLocalTime().toString(Qt::SystemLocaleLongDate)), 6 );
75 VFS::LOG( QString(
"Created non-expiring '%1' session for user '%2'").arg(
_type).arg(
_user), 6 );
79 timerID = startTimer( 60000, Qt::VeryCoarseTimer );
89 VFS::WARN(
"Null or non-node client provided to session.");
123 , _initialized(false)
127 _created = QDateTime::fromSecsSinceEpoch( (qint64) data[
"created"].toDouble() );
129 _expires = QDateTime::fromSecsSinceEpoch( (qint64) QDateTime::currentSecsSinceEpoch() + 590 );
130 _address = data[
"address"].toString();
131 _user = data[
"user"].toString();
132 _type = data[
"type"].toString();
133 _groups = data[
"groups"].toArray();
134 _home = data[
"home"].toString();
137 _tokens = data[
"tokens"].toObject();
141 VFS::LOG( QString(
"Created '%1' session for user '%2' which will expire on %3").arg(
_type).arg(
_user).arg(
_expires.toLocalTime().toString(Qt::SystemLocaleLongDate)), 8 );
143 VFS::LOG( QString(
"Created non-expiring '%1' session for user '%2'").arg(
_type).arg(
_user), 8 );
147 timerID = startTimer( 60000, Qt::VeryCoarseTimer );
157 VFS::WARN(
"Null or non-node client provided to session.");
181 t[
"preferencesPath"] =
"";
182 t[
"interfacePath"] =
"";
250 VFS::LOG( QString(
"Registered VFS_session type '%1'.").arg(type), 8 );
254 VFS::WARN( QString(
"Can't register VFS_session type '%1'. It is already registered.").arg(type) );
270 QMutexLocker l(&
_lock);
281 if (key ==
"uidnumber")
287 if (key ==
"realname")
293 return QJsonValue::Null;
314 return QJsonValue::Null;
356 QMutexLocker l(&
_lock);
360 o[
"created"] =
_created.toSecsSinceEpoch();
361 o[
"expires"] =
_expires.toSecsSinceEpoch();
388 QDateTime n = QDateTime::currentDateTimeUtc();
405 return _authtoken.split(
"/",Qt::SkipEmptyParts).last();
417 QMutexLocker l(&
_lock);
437 QMutexLocker l(&
_lock);
439 foreach(QString g, groups)
466 QMutexLocker l(&
_lock);
470 e =
_expires.toString(Qt::SystemLocaleLongDate);
474 QString s = QString(
"user: %1\ntype: %2\naddress: %6\nexpires: %5\nhome: %3\nauth: %4\ngroups: %7").arg(
_user).arg(
_type).arg(
_home).arg(
_authpath).arg(e).arg(
_address).arg(QString(QJsonDocument(
_groups).
toJson()).replace(
"\n",
""));
490 if (event->timerId() ==
timerID)
492 if ( QDateTime::currentDateTimeUtc() >=
_expires)
521 QMutexLocker l(&
_lock);
524 QJsonObject::iterator i;
546 tm[
"createIfMissing"] =
true;
549 td[
"username"] =
_user;
551 td[
"expire"] =
_expires.toSecsSinceEpoch()/60;
564 m[
"createIfMissing"] =
true;
565 m[
"container"] =
true;
569 m[
"container"] =
true;
573 QString s = i.value().toString();
589 m[
"container"] =
false;
593 QString s = i.value().toString();
597 if (!s.endsWith(
"/"))
603 if (d.contains(i.key()))
604 { q->
_data.setObject( d[i.key()].toObject() );
608 {
VFS::WARN( QString(
"No key '%2' in sessionDefaults for '%1' when creating '%3'").arg(
_type).arg(i.key()).arg(s) );
663 QJsonObject d = r->
_data.object();
664 _uidnumber = (quint16) d[
"uidnumber"].toInt( 0 );
666 _groups = d[
"groups"].toArray();
674 o[
"accepted"] =
true;
675 o[
"username"] =
_user;
680 r->
_data.setObject(o);
703 QMutexLocker l(&
_lock);
705 QRegularExpression tokenRX(
"@([^@]+)@");
706 QRegularExpressionMatch match;
708 QJsonObject::iterator i;
710 while (i != data.end())
712 QString key = i.key();
713 QString value = i.value().toString();
715 match = tokenRX.match(value);
716 while (match.hasMatch())
718 QString matched = match.captured(1);
720 QString t =
_tokens.contains(matched) ?
_tokens[matched].toString() :
"[unresolved: "+matched+
"]";
721 value = value.replace(
"@"+matched+
"@", t );
722 match = tokenRX.match(value);
772 o[
"username"] =
_user;
776 o[
"created"] =
_created.toMSecsSinceEpoch();
777 o[
"expires"] =
_expires.toMSecsSinceEpoch();
786 r->
_data.setObject(o);
828 if (r->
_data.isEmpty())
854 QJsonObject d = r->
_data.object();
855 l[
"username"] = d[
"username"];
856 l[
"uidnumber"] = d[
"uidnumber"];
857 l[
"realname"] = d[
"realname"];
858 l[
"groups"] = d[
"groups"];
863 VFS::WARN(
"Could not populate user information.");
static char * get(QString which="")
Fetch an icon from the library.
VFS_node is the base class from which all other VFS_node classes derive.
virtual VFS_request * createRequest(VFS_request::requestType type, QString path, QString user="unknown", QJsonDocument data=QJsonDocument(), QJsonObject metadata=QJsonObject(), bool dontDelete=false)
Create a new VFS_request with this object as _origin.
virtual void issueResponse(VFS_request *t)
Once a request has been completed, issue a response.
virtual void metadata(VFS_request *r)
Fetch the metadata of this node.
static bool __removeNode(VFS_node *)
Remove a node from the global registry.
virtual void issueRequest(VFS_request *t)
A convenience function.
static bool __isNode(VFS_node *)
Check to see if a node is in the global registry.
QMutex _lock
A recursive mutex that is local to this node.
virtual void rm(VFS_request *r)
Remove a child entry from a node, or the node itself.
void finished(bool andDelete=false)
Emitted if a thread fails to create its node, or a node is rm()'d, or any other reason a node has com...
virtual void receiveResponse(VFS_request *t)
Once a VFS_request has been completed, a response will be issued back to its _origin.
The base class for all requests between nodes.
@ read
read full contents (4)
@ write
write full contents (5)
@ subscribe
subscribe to a path (9)
QString _initialPath
the target path when the request was made (relative to the responder)
QString _user
who initiated this request, mostly for logging
VFS_session * _session
The session associated with this request. This is an optional value, and care should be taken to chec...
QString _reason
if something (probably bad) happened, this is the reason
bool _isCallback
whether or not to issue a response (IE, another request is chained to this request,...
bool _success
if the request was successfully completed
QJsonDocument _data
the request payload
QJsonObject _metadata
the request payload
The VFS_session object represents a single session.
QString _realname
The real name of the user associated with this session.
virtual void timerEvent(QTimerEvent *event)
The timer event used to check if the session is still valid.
bool _initialized
false until initialization is complete
static QMutex _sessionTypesLock
A mutex to protect thread safety during initialization.
QDateTime _expires
The Expiration date/time of this session.
virtual void rm(VFS_request *r)
Remove (delete) this node, and also the _client associated with it.
QJsonObject _tokens
The tokens available to resolve session values.
static bool _initializedTypes
Whether or not VFS_session static variables have been initialized.
virtual QByteArray icon()
Return the user icon from VFS_icons.
static QJsonObject _sessionTypes
The registered session types. The entries here will be de-tokenized on read()
QQueue< VFS_request * > _initializers
The list of initialization requests which must be empty for _initialized to become true.
virtual void applyDiff(VFS_request *r)
Receive diffs from our subscriptions.
virtual void receiveResponse(VFS_request *t)
VFS_sessionManager::receiveResponse.
VFS_sessionManager * _manager
The sessionManager tracking this session.
QJsonObject resolveTokens(QJsonObject _data)
Resolve the tokens for a session type based on variables gathered in the _tokens object.
virtual void initializeUser(VFS_request *r)
Check and create session files for a user.
static QJsonValue fetchDefaultSessionValue(QString type, QString key)
Fetch a session type's default value by key, as registered by registerSessionType()
qint64 secondsToExpire()
Given the current time, calculate the number of seconds until this session will expire.
QString _type
The session or client type.
static QJsonObject _sessionDefaults
The default values, per type, per entry.
bool isMemberOf(QString group)
Check if this session's group list includes the specified group.
QJsonObject toJson()
Return a json object with the salient data from this session.
quint16 _uidnumber
The uidnumber of the user associated with this session.
QString _user
The username associated with this session.
QJsonArray _groups
The list of groups this user is a member of.
QString _authpath
The VFS_auth path that authenticated this session.
QString _address
The remote address of the client.
VFS_node * _client
The client this session is for, which will usually be a subclass of VFS_websocket_client.
static VFS_session * fetchSession(QString address)
Try to resolve and cast a string address to a VFS_session.
virtual QString reportDetails()
Report data about a connected client.
QString _home
The VFS path to this user's home directory.
void dequeueInitializer()
Remove the first initializer item from _initializers and call issueRequest().
QString _authtoken
The authtoken VFS path associated with this session, which corresponds to an entry at VFS_sessionMana...
QJsonObject _sessionData
The resolved session data for this user type.
QDateTime _created
The creation date/time of this session.
QString authtoken()
Return the file component of the _authtoken path.
static bool registerSessionType(QString type, QJsonObject definition, QJsonObject defaults=QJsonObject())
Register a new session type by name.
static void initializeTypes()
Initialize the VFS_session::_sessionTypes member.
int timerID
The ID of the session timer.
virtual bool isContainer()
A VFS_session cannot have children.
virtual void read(VFS_request *r)
Read the session data.
QJsonValue fetchSessionValue(QString key)
Fetch a value from the _sessionData, by key.
VFS_sessionManager manages connected clients and their session data.
virtual QString getCWD()
Return the manager's _cwd value.
static void LOG(QString message, int level=0, QString user="server")
Send a message to the VFS::_messages VFS_stream.
static void ERROR(QString message, int level=0, QString user="server")
Send a message to the VFS::_errors VFS_stream.
static void WARN(QString message, int level=0, QString user="server")
Send a message to the VFS::_warnings VFS_stream.
setter type
a setter DOCME
QJsonObject jsonResource(QString resource, bool *ok=nullptr)
Fetch the contents of a Qt resource as a QJsonObject.