Newer
Older
/*******************************************************************************
* Copyright (c) 2025, Jan Koester jan.koester@gmx.net
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/
authdb::UserData::UserData(uuid_t id) : AuthData(id){
username=nullptr;
firstname=nullptr;
lastname=nullptr;
mail=nullptr;
pwhash=nullptr;
avatar=nullptr;
authdb::UserData::~UserData(){
}
void authdb::UserData::setFirstName(const char* src){
setAuthData<char,char>(DataType::UserData,StorageType::TextStorage,this,firstname,"firstname",src, strlen(src)+1);
return getAuthData<char>(DataType::UserData,StorageType::TextStorage,this,firstname,"firstname");
setAuthData<char,char>(DataType::UserData,StorageType::TextStorage,this,lastname,"lastname",src,strlen(src)+1);
return getAuthData<char>(DataType::UserData,StorageType::TextStorage,this,lastname,"lastname");
setAuthData<char,char>(DataType::UserData,StorageType::TextStorage,this,mail,"mail",src,strlen(src)+1);
return getAuthData<char>(DataType::UserData,StorageType::TextStorage,this,mail,"mail");
setAuthData<char,char>(DataType::UserData,StorageType::TextStorage,this,pwhash,"pwhash",src,strlen(src)+1);
return getAuthData<char>(DataType::UserData,StorageType::TextStorage,this,pwhash,"pwhash");
setAuthData<char,char>(DataType::UserData,StorageType::TextStorage,this,username,"username",src,strlen(src)+1);
return getAuthData<char>(DataType::UserData,StorageType::TextStorage,this,username,"username");
void authdb::UserData::setAvatar(const std::vector<char>& src){
setAuthData<char,char>(DataType::UserData,StorageType::BinaryStorage,this,
void authdb::UserData::getAvatar(std::vector<char>& dest){
if(avatar && avatar->data)
std::copy(avatar->data,avatar->data+avatar->datasize,
if(exits(backend,&dat->uid,dat->getUsername(),exi)){
switch(exi){
case 1:
throw authdb::AuthBackendError("Create User: Username already Exists aborting!");
throw authdb::AuthBackendError("Create User: UUID already Exists aborting!");
for(std::shared_ptr<AuthData> curec(dat); curec; curec=curec->next()){
backend.write((unsigned char*)curec->Data.get(),sizeof(struct AuthData::Record));
backend.write((unsigned char*)curec->Data->data,curec->Data->datasize);
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
bool authdb::User::exits(AuthBackend& backend, uuid_t* uid, const char* name,int &ret){
authdb::AuthData::Record cur;
size_t rd=sizeof(authdb::AuthHeader),end=backend.end();
while(rd<end){
backend.setPos(rd);
backend.read((unsigned char*)&cur,sizeof(AuthData::Record));
rd=backend.getPos()+cur.datasize;
if(strcmp(cur.fieldname,"username")==0 ){
cur.data=new char[cur.datasize];
backend.read((unsigned char*)cur.data,cur.datasize);
if(name && strcmp(cur.data,name) == 0){
delete[] cur.data;
ret=1;
return true;
}else if(uid && uuid_compare(cur.uuid,*uid)==0){
delete[] cur.data;
ret=2;
return true;
}
delete[] cur.data;
}
}
ret=-1;
return false;
}
void authdb::User::edit(AuthBackend &backend,class UserData& dat, size_t &pos){
void authdb::User::remove(AuthBackend &backend,const uuid_t uid){
void authdb::User::info(AuthBackend &backend, class UserData& dat, size_t &pos){
throw AuthBackendError("user info uid required!");
while(pos<end){
authdb::AuthData::Record *cur=new AuthData::Record;
backend.setPos(pos);
backend.read((unsigned char*)cur,sizeof(AuthData::Record));
if(cur->type == UserData && uuid_compare(dat.uid,cur->uuid) == 0
&& strcmp(cur->fieldname,"username")==0
){
if(strcmp(dat.Data->fieldname,"username")==0){
dat.username=dat.Data;
}else if(strcmp(dat.Data->fieldname,"firstname")==0){
dat.firstname=dat.Data;
}else if(strcmp(dat.Data->fieldname,"lastname")==0){
dat.lastname=dat.Data;
}else if(strcmp(dat.Data->fieldname,"mail")==0){
dat.mail=dat.Data;
}else if(strcmp(dat.Data->fieldname,"pwhash")==0){
dat.pwhash=dat.Data;
}else if(strcmp(dat.Data->fieldname,"avatar")==0){
dat.avatar=dat.Data;