7152-devToTest_2414 #2228

Merged
alexm merged 636 commits from 7152-devToTest_2414 into test 2024-03-28 08:26:34 +00:00
5 changed files with 197 additions and 132 deletions
Showing only changes of commit f2ad069186 - Show all commits

View File

@ -0,0 +1,27 @@
module.exports = Self => {
Self.remoteMethodCtx('shareToken', {
description: 'Returns token to view files or images and share it',
accessType: 'WRITE',
accepts: [],
returns: {
type: 'Object',
root: true
},
http: {
path: `/shareToken`,
verb: 'GET'
}
});
Self.shareToken = async function(ctx) {
const {accessToken: token} = ctx.req;
const user = await Self.findById(token.userId);
const multimediaToken = await user.accessTokens.create({
scopes: ['read:multimedia']
});
return {multimediaToken};
};
};

View File

@ -0,0 +1,27 @@
const {models} = require('vn-loopback/server/server');
fdescribe('Share Token', () => {
let ctx = null;
beforeAll(async() => {
const unAuthCtx = {
req: {
headers: {},
connection: {
remoteAddress: '127.0.0.1'
},
getLocale: () => 'en'
},
args: {}
};
let login = await models.VnUser.signIn(unAuthCtx, 'salesAssistant', 'nightmare');
let accessToken = await models.AccessToken.findById(login.token);
ctx = {req: {accessToken: accessToken}};
});
it('should renew token', async() => {
const multimediaToken = await models.VnUser.shareToken(ctx);
expect(Object.keys(multimediaToken).length).toEqual(1);
expect(multimediaToken.multimediaToken.userId).toEqual(ctx.req.accessToken.userId);
expect(multimediaToken.multimediaToken.scopes[0]).toEqual('read:multimedia');
});
});

View File

@ -13,6 +13,7 @@ module.exports = function(Self) {
require('../methods/vn-user/privileges')(Self); require('../methods/vn-user/privileges')(Self);
require('../methods/vn-user/validate-auth')(Self); require('../methods/vn-user/validate-auth')(Self);
require('../methods/vn-user/renew-token')(Self); require('../methods/vn-user/renew-token')(Self);
require('../methods/vn-user/share-token')(Self);
require('../methods/vn-user/update-user')(Self); require('../methods/vn-user/update-user')(Self);
Self.definition.settings.acls = Self.definition.settings.acls.filter(acl => acl.property !== 'create'); Self.definition.settings.acls = Self.definition.settings.acls.filter(acl => acl.property !== 'create');
@ -167,11 +168,7 @@ module.exports = function(Self) {
console.warn(err); console.warn(err);
} }
const multimediaToken = await token.user().accessTokens.create({ return {token: token.id, ttl: token.ttl};
scopes: ['read:multimedia']
});
return {token: token.id, ttl: token.ttl, multimediaToken};
}; };
Self.userUses = function(user) { Self.userUses = function(user) {

View File

@ -1,129 +1,140 @@
{ {
"name": "VnUser", "name": "VnUser",
"base": "User", "base": "User",
"validateUpsert": true, "validateUpsert": true,
"options": { "options": {
"mysql": { "mysql": {
"table": "account.user" "table": "account.user"
} }
}, },
"mixins": { "mixins": {
"Loggable": true "Loggable": true
}, },
"resetPasswordTokenTTL": "604800", "resetPasswordTokenTTL": "604800",
"properties": { "properties": {
"id": { "id": {
"type": "number", "type": "number",
"id": true "id": true
}, },
"name": { "name": {
"type": "string", "type": "string",
"required": true "required": true
}, },
"username": { "username": {
"type": "string" "type": "string"
}, },
"roleFk": { "roleFk": {
"type": "number", "type": "number",
"mysql": { "mysql": {
"columnName": "role" "columnName": "role"
} }
}, },
"nickname": { "nickname": {
"type": "string" "type": "string"
}, },
"lang": { "lang": {
"type": "string" "type": "string"
}, },
"active": { "active": {
"type": "boolean" "type": "boolean"
}, },
"email": { "email": {
"type": "string" "type": "string"
}, },
"emailVerified": { "emailVerified": {
"type": "boolean" "type": "boolean"
}, },
"created": { "created": {
"type": "date" "type": "date"
}, },
"updated": { "updated": {
"type": "date" "type": "date"
}, },
"image": { "image": {
"type": "string" "type": "string"
}, },
"hasGrant": { "hasGrant": {
"type": "boolean" "type": "boolean"
}, },
"passExpired": { "passExpired": {
"type": "date" "type": "date"
}, },
"twoFactor": { "twoFactor": {
"type": "string" "type": "string"
} }
}, },
"relations": { "relations": {
"role": { "role": {
"type": "belongsTo", "type": "belongsTo",
"model": "VnRole", "model": "VnRole",
"foreignKey": "roleFk" "foreignKey": "roleFk"
}, },
"roles": { "roles": {
"type": "hasMany", "type": "hasMany",
"model": "RoleRole", "model": "RoleRole",
"foreignKey": "role", "foreignKey": "role",
"primaryKey": "roleFk" "primaryKey": "roleFk"
}, },
"emailUser": { "emailUser": {
"type": "hasOne", "type": "hasOne",
"model": "EmailUser", "model": "EmailUser",
"foreignKey": "userFk" "foreignKey": "userFk"
}, },
"worker": { "worker": {
"type": "hasOne", "type": "hasOne",
"model": "Worker", "model": "Worker",
"foreignKey": "id" "foreignKey": "id"
}, },
"userConfig": { "userConfig": {
"type": "hasOne", "type": "hasOne",
"model": "UserConfig", "model": "UserConfig",
"foreignKey": "userFk" "foreignKey": "userFk"
} }
}, },
"acls": [ "acls": [
{ {
"property": "signIn", "property": "signIn",
"accessType": "EXECUTE", "accessType": "EXECUTE",
"principalType": "ROLE", "principalType": "ROLE",
"principalId": "$everyone", "principalId": "$everyone",
"permission": "ALLOW" "permission": "ALLOW"
}, { },
"property": "recoverPassword", {
"accessType": "EXECUTE", "property": "recoverPassword",
"principalType": "ROLE", "accessType": "EXECUTE",
"principalId": "$everyone", "principalType": "ROLE",
"permission": "ALLOW" "principalId": "$everyone",
}, { "permission": "ALLOW"
"property": "validateAuth", },
"accessType": "EXECUTE", {
"principalType": "ROLE", "property": "validateAuth",
"principalId": "$everyone", "accessType": "EXECUTE",
"permission": "ALLOW" "principalType": "ROLE",
}, { "principalId": "$everyone",
"property": "privileges", "permission": "ALLOW"
"accessType": "*", },
"principalType": "ROLE", {
"principalId": "$authenticated", "property": "privileges",
"permission": "ALLOW" "accessType": "*",
}, { "principalType": "ROLE",
"property": "renewToken", "principalId": "$authenticated",
"accessType": "WRITE", "permission": "ALLOW"
"principalType": "ROLE", },
"principalId": "$authenticated", {
"permission": "ALLOW" "property": "renewToken",
} "accessType": "WRITE",
], "principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"property": "shareToken",
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
}
],
"scopes": { "scopes": {
"preview": { "preview": {
"fields": [ "fields": [
@ -140,7 +151,7 @@
"hasGrant", "hasGrant",
"realm", "realm",
"email", "email",
"emailVerified" "emailVerified"
] ]
} }
} }

View File

@ -83,15 +83,18 @@ export default class Auth {
} }
onLoginOk(json, now, remember) { onLoginOk(json, now, remember) {
this.vnToken.set(json.data.token, json.data.multimediaToken.id, now, json.data.ttl, remember); return this.$http.get('VnUsers/ShareToken', {
headers: {Authorization: json.data.token}
return this.loadAcls().then(() => { }).then(({data}) => {
let continueHash = this.$state.params.continue; this.vnToken.set(json.data.token, data.multimediaToken.id, now, json.data.ttl, remember);
if (continueHash) this.loadAcls().then(() => {
this.$window.location = continueHash; let continueHash = this.$state.params.continue;
else if (continueHash)
this.$state.go('home'); this.$window.location = continueHash;
}); else
this.$state.go('home');
});
}).catch(() => {});
} }
logout() { logout() {