Changed .add to .create

This commit is contained in:
2024-07-31 20:02:51 -04:00
parent 75a486251d
commit fb9a19d5f0
10 changed files with 21 additions and 137 deletions
-50
View File
@@ -1,50 +0,0 @@
'use strict';
const {User} = require('./user');
const {Token, AuthToken} = require('./token');
let Auth = {}
Auth.errors = {}
Auth.errors.login = function(){
let error = new Error('LoginFailed');
error.name = 'LoginFailed';
error.message = `Invalid Credentials, login failed.`;
error.status = 401;
return error;
}
Auth.login = async function(data){
try{
let user = await User.login(data);
let token = await AuthToken.add(user);
return {user, token}
}catch(error){
throw this.errors.login();
}
};
Auth.checkToken = async function(data){
try{
let token = await AuthToken.get(data);
if(token.is_valid){
return await User.get(token.created_by);
}
}catch(error){
throw this.errors.login();
}
};
Auth.logOut = async function(data){
try{
let token = await AuthToken.get(data);
await token.remove();
}catch(error){
throw error;
}
}
module.exports = {Auth, AuthToken};
+4 -4
View File
@@ -48,7 +48,7 @@ class Host extends Table{
console.log('addCache, corrent parent?', parentOBJ.wildcard_parent || parentOBJ.host)
console.log('addCache, got parent', parentOBJ)
await this.add({
await this.create({
...parentOBJ,
host: host,
is_cache: true,
@@ -56,7 +56,7 @@ class Host extends Table{
wildcard_parent: parentOBJ.host
}, true);
await Cached.add({
await Cached.create({
host: host,
parent: parentOBJ.host
});
@@ -86,7 +86,7 @@ class Host extends Table{
static async add(data, ...args){
try{
let out = await super.add(data, ...args)
let out = await super.create(data, ...args)
await this.buildLookUpObj()
if(out.is_wildcard) await out.createWildcardCert()
@@ -272,7 +272,7 @@ module.exports = {Host: ModelPs(Host)};
try{
await Host.lookUpReady();
// let res = await Host.add({
// let res = await Host.create({
// host: '*.test.holycore.quest',
// ip: '192.168.1.47',
// 'created_by': 'william',
+1 -1
View File
@@ -39,7 +39,7 @@ class AuthToken extends Token{
static async add(data){
data.created_by = data.username;
return super.add(data)
return super.create(data)
}
-65
View File
@@ -126,71 +126,6 @@ User.exists = async function(data){
}
};
// User.add = async function(data) {
// try{
// data = objValidate.processKeys(this.keyMap, data);
// let systemUser = await linuxUser.addUser(data.username);
// await require('util').promisify(setTimeout)(500)
// let systemUserPassword = await linuxUser.setPassword(data.username, data.password);
// return this.get(data.username);
// }catch(error){
// if(error.message.includes('exists')){
// let error = new Error('UserNameUsed');
// error.name = 'UserNameUsed';
// error.message = `PAM:${data.username} already exists`;
// error.status = 409;
// throw error;
// }
// throw error;
// }
// };
// User.addByInvite = async function(data){
// try{
// let token = await InviteToken.get(data.token);
// if(!token.is_valid){
// let error = new Error('Token Invalid');
// error.name = 'Token Invalid';
// error.message = `Token is not valid or as allready been used. ${data.token}`;
// error.status = 401;
// throw error;
// }
// let user = await this.add(data);
// if(user){
// await token.consume({claimed_by: user.username});
// return user;
// }
// }catch(error){
// throw error;
// }
// };
// User.remove = async function(data){
// try{
// return await linuxUser.removeUser(this.username);
// }catch(error){
// throw error;
// }
// };
// User.setPassword = async function(data){
// try{
// await linuxUser.setPassword(this.username, data.password);
// return this;
// }catch(error){
// throw error;
// }
// };
User.invite = async function(){
try{
let token = await InviteToken.add({created_by: this.username});
+1 -1
View File
@@ -68,7 +68,7 @@ User.exists = async function(data){
}
};
User.add = async function(data) {
User.create = async function(data) {
try{
data = objValidate.processKeys(this.keyMap, data);
let systemUser = await linuxUser.addUser(data.username);
+4 -4
View File
@@ -25,7 +25,7 @@ class User extends Table{
data['backing'] = data['backing'] || 'redis';
return await super.add(data)
return await super.create(data)
}catch(error){
throw error;
@@ -44,7 +44,7 @@ class User extends Table{
throw error;
}
let user = await this.add(data);
let user = await this.create(data);
if(user){
await token.consume({claimed_by: user.username});
@@ -69,7 +69,7 @@ class User extends Table{
/* async invite(){
try{
let token = await InviteToken.add({created_by: this.username});
let token = await InviteToken.create({created_by: this.username});
return token;
@@ -114,7 +114,7 @@ module.exports = {User};
let user = await User.get(defaultUser);
}catch(error){
try{
let user = await User.add({
let user = await User.create({
username:defaultUser,
password: defaultUser,
created_by:defaultUser
+1 -1
View File
@@ -18,7 +18,7 @@ router.get('/', async function(req, res, next){
router.post('/', async function(req, res, next){
try{
req.body.created_by = req.user.username;
let item = await Model.add(req.body);
let item = await Model.create(req.body);
return res.json({
message: `"${item[Model._key]}" added.`,
+2 -6
View File
@@ -83,11 +83,7 @@ class Table{
return out;
}
static async create(...args){
return this.add(...args);
}
static async add(data){
static async create(data){
// Add a entry to this redis table.
try{
// Validate the passed data by the keyMap schema.
@@ -140,7 +136,7 @@ class Table{
// Create a new record for the updated entry. If that succeeds,
// delete the old recored
let newObject = await this.constructor.add(newData);
let newObject = await this.constructor.create(newData);
if(newObject){
await this.remove();
+3 -3
View File
@@ -110,8 +110,8 @@
});
});
app.subscribe(/./g, function(data, topic){
console.log('topic', data);
app.subscribe(/^model:Host/, function(data, topic){
console.log(topic, data);
});
//search bar html event logic stolen from here
@@ -299,7 +299,7 @@
</div>
<div class="card-header actionMessage" style="display:none"></div>
<div class="card-header search-wrapper">
<div class="card-body search-wrapper">
<label class="control-label" for="search" style="margin-left: -5px;">Search Hosts: </label>
<input type="search" id="search" host-search>
</div>
+5 -2
View File
@@ -52,7 +52,7 @@
<div class="row" style="display:none">
<div class="col-md-4">
<div class="card shadow-lg">
<div class="card-header text-center">
<span class="card-icon float-start">
<i class="fa-solid fa-user-plus"></i>
@@ -83,7 +83,10 @@
<label class="control-label">Again</label>
<input type="password" class="form-control" name="passwordMatch" placeholder="Retype password" validate="eq:password"/>
</div>
<button type="button" onclick="formAJAX(this)" class="btn btn-info">Add</button>
<hr />
<button type="button" onclick="formAJAX(this)" class="btn btn-info">
Add
</button>
</form>
</div>
</div>