/******************************************************************************* * 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 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 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. *******************************************************************************/ #include "types.h" #include "user.h" authdb::UserData::UserData(uuid_t id) : AuthData(id){ username=nullptr; firstname=nullptr; lastname=nullptr; mail=nullptr; pwhash=nullptr; avatar=nullptr; uuid_copy(uid,id); } authdb::UserData::~UserData(){ } void authdb::UserData::setFirstName(const char* src){ setAuthData(DataType::UserData,StorageType::TextStorage,this,firstname,"firstname",src, strlen(src)+1); } const char *authdb::UserData::getFirstname(){ return getAuthData(DataType::UserData,StorageType::TextStorage,this,firstname,"firstname"); } void authdb::UserData::setLastName(const char* src){ setAuthData(DataType::UserData,StorageType::TextStorage,this,lastname,"lastname",src,strlen(src)+1); } const char * authdb::UserData::getLastname(){ return getAuthData(DataType::UserData,StorageType::TextStorage,this,lastname,"lastname"); } void authdb::UserData::setMail(const char* src){ setAuthData(DataType::UserData,StorageType::TextStorage,this,mail,"mail",src,strlen(src)+1); } const char *authdb::UserData::getMail(){ return getAuthData(DataType::UserData,StorageType::TextStorage,this,mail,"mail"); } void authdb::UserData::setPwHash(const char* src){ setAuthData(DataType::UserData,StorageType::TextStorage,this,pwhash,"pwhash",src,strlen(src)+1); } const char * authdb::UserData::getPwHash(){ return getAuthData(DataType::UserData,StorageType::TextStorage,this,pwhash,"pwhash"); } void authdb::UserData::setUserName(const char* src){ setAuthData(DataType::UserData,StorageType::TextStorage,this,username,"username",src,strlen(src)+1); } const char * authdb::UserData::getUsername(){ return getAuthData(DataType::UserData,StorageType::TextStorage,this,username,"username"); } void authdb::UserData::setAvatar(const std::vector& src){ setAuthData(DataType::UserData,StorageType::BinaryStorage,this, avatar,"avatar",src.data(),src.size()); } void authdb::UserData::getAvatar(std::vector& dest){ if(avatar && avatar->data) std::copy(avatar->data,avatar->data+avatar->datasize, std::back_inserter(dest)); else throw(authdb::AuthBackendError("Avatar data nout found !")); } void authdb::User::create(AuthBackend &backend,class UserData* dat){ int exi=0; if(exits(backend,&dat->uid,dat->getUsername(),exi)){ switch(exi){ case 1: throw authdb::AuthBackendError("Create User: Username already Exists aborting!"); default: throw authdb::AuthBackendError("Create User: UUID already Exists aborting!"); } } size_t wd=backend.end(); for(std::shared_ptr curec(dat); curec; curec=curec->next()){ curec->Data->type=UserData; backend.setPos(wd); backend.write((unsigned char*)curec->Data.get(),sizeof(struct AuthData::Record)); backend.write((unsigned char*)curec->Data->data,curec->Data->datasize); wd=backend.end(); } } 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(rddatasize; if(cur->type == UserData && uuid_compare(dat.uid,cur->uuid) == 0 && strcmp(cur->fieldname,"username")==0 ){ if(!getRecord(backend,dat,DataType::UserData)) continue; 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; } } delete cur; } }