Commit 12cd7c0c authored by Matheus Horstmann's avatar Matheus Horstmann 🐴 Committed by Bruno Meyer

Criar os Roles necessários

parent 80cf3966
......@@ -19,9 +19,10 @@ ds.automigrate(lbTables, function(err) {
async.waterfall([
loadModelFromOldDB(old_db.get_salas, app.models.Sala, LOG_CREATE),
loadModelFromOldDB(old_db.get_blocos, app.models.Bloco, LOG_CREATE),
criaUser,
criaDisciplina,
criaEquivalencia,
criaUsuario,
criaSecretario,
criaRoles,
atribuiRoles,
criaRecursodesala,
criaTipodesala,
loadModelFromOldDB(old_db.get_professores, app.models.Professor, LOG_CREATE),
......@@ -29,8 +30,8 @@ ds.automigrate(lbTables, function(err) {
loadModelFromOldDB(old_db.get_setores, app.models.Setor, LOG_CREATE),
loadModelFromOldDB(old_db.get_disciplinas, app.models.Disciplina, LOG_CREATE),
loadModelFromOldDB(old_db.get_equivalenciasDisciplinas, app.models.Equivalenciadisciplina, LOG_CREATE),
loadModelFromOldDB(old_db.get_cursos, app.models.Curso, LOG_CREATE),
loadModelFromOldDB(old_db.get_cursos, app.models.Curso, LOG_CREATE),
loadRelationsFromOldDB(old_db.get_cursoDisciplina, app.models.Curso, "disciplinas", LOG_CREATE),
],
function(err) {
......@@ -80,9 +81,9 @@ function loadRelationsFromOldDB(oldDataAccess, model, modelRelationName, create_
return function(cb){
oldDataAccess.then(data => {
async.each(data, (instance, callback) => {
if(create_log){
console.log('Trying to insert relation on ',
console.log('Trying to insert relation on ',
modelRelationName,':', instance
);
}
......@@ -137,34 +138,106 @@ function criaTipodesala(cb){
});
}
function criaSecretario(cb){
var secretarios = [
{email:'secretario_curso@secretario.com',vinculo: 'curso'},
{email:'secretario_dpto@secretario.com',vinculo: 'departamento'},
{email:'secretario_setor@secretario.com',vinculo: 'setor'}
]
function criaUser(cb){
app.models.Secretario.replaceOrCreate([
{username: 'admin', email: 'admin@admin.com', password: '123mudar'},
{username: 'secretario1', email: 'secretario1@secretario.com', password: '123mudar'},
{username: 'secretario2', email: 'secretario2@secretario.com', password: '123mudar'}
],function(err, users) {
if (err) throw err;
if(LOG_CREATE) console.log('Created users:', users);
app.models.Role.create({
name: 'admin'
}, function(err, role) {
async.each(secretarios, function(secretario, callback) {
app.models.Usuario.findOne({where:{email: secretario.email}}, function(err, user){
user.secretario.create( {vinculo: secretario.vinculo},function(err,secre) {
if (err) throw err;
if(LOG_CREATE) console.log('Created Secretario:', secre);
callback(err);
});
});
}, function(err) {
if (err) throw err;
cb(err);
});
}
function criaRoles(cb){
var roles = [
{name: 'admin'},
{name: 'comissao'},
{name: 'secretario_setor'},
{name: 'secretario_dpto'},
{name: 'secretario_curso'}
];
async.each(roles, function(role, callback) {
app.models.Role.create(role, function(err, model) {
if (err) throw err;
if(LOG_CREATE) console.log('Created role:', model);
callback(err);
});
}, function(err) {
if (err) throw err;
cb(err);
});
}
function atribuiRoles(cb){
var atribuicoes = [
{email:'admin@admin.com',role: 'admin'},
{email:'comissao@comissao.com',role: 'comissao'},
{email:'secretario_setor@secretario.com',role: 'secretario_setor'},
{email:'secretario_dpto@secretario.com',role: 'secretario_dpto'},
{email:'secretario_curso@secretario.com',role: 'secretario_curso'}
]
async.each(atribuicoes, function(atribuicao, callback) {
app.models.Usuario.findOne({where:{email: atribuicao.email}}, function(err, user){
user.setRole(atribuicao.role,function(err,data){
if (err) throw err;
if(LOG_CREATE) console.log('Assign '+atribuicao.role+ ' Role to initial User:', user);
callback(err);
});
});
}, function(err) {
if (err) throw err;
if(LOG_CREATE) console.log('Created role:', role);
role.principals.create({
principalType: app.models.RoleMapping.USER,
principalId: users[0].id
}, function(err, principal) {
cb(err);
});
}
function criaUsuario(cb){
var usuarios = [
{username: 'admin',nome:'Administrador do Sistema', email: 'admin@admin.com', password: '123mudar'},
{username: 'comissao',nome:'Comissão de Ensalamento', email: 'comissao@comissao.com', password: '123mudar'},
{username: 'secretario_setor',nome:'Secretário do Setor', email: 'secretario_setor@secretario.com', password: '123mudar'},
{username: 'secretario_dpto',nome:'Secretário de Departamento,', email: 'secretario_dpto@secretario.com', password: '123mudar'},
{username: 'secretario_curso',nome:'Secretário de Curso', email: 'secretario_curso@secretario.com', password: '123mudar'}
];
async.each(usuarios, function(usuario, callback) {
app.models.Usuario.create(usuario, function(err, model) {
if (err) throw err;
if(LOG_CREATE) console.log('Assign admin Role to initial User:', users[0].email);
cb(err);
callback(err);
if(LOG_CREATE) console.log('Created user:', model);
});
}, function(err) {
if (err) throw err;
cb(err);
});
});
}
function criaEquivalencia(cb){
app.models.Disciplina.find({id:1}, function(err, disc){
app.models.Disciplina.find({where:{id:1}}, function(err, disc){
disc[0].addEquivalencia(2,function(err,data){
if (err) throw err;
if(LOG_CREATE) console.log('Created Equivalenciadisciplina:', disc);
......
......@@ -103,8 +103,8 @@
## Recursodesala
- Public: `False`
- Description: This model contains the resources listage of rooms.
In future, this can be use to allocate a room to disciplinas.
In this case, a disciplina must gain a room that meet this
In future, this can be use to allocate a room to disciplinas.
In this case, a disciplina must gain a room that meet this
resources needs.
- Attributes
* descricao : **string** `Required`
......@@ -121,8 +121,8 @@
## Tipodesala
- Public: `False`
- Description: This model contains the types of rooms listage.
In future, this can be use to allocate a room to disciplinas.
In this case, a disciplina must gain a room that meet this
In future, this can be use to allocate a room to disciplinas.
In this case, a disciplina must gain a room that meet this
room type need.
- Attributes `idInjection`=`false`
* nome : **string** `Required`, default=`'normal'`, `id`
......@@ -201,14 +201,13 @@
* Has Many : **Curso** as `setorCod`
## Secretario
- Public: `True`
- Description: This model extend `User`
- Public: `False`
- Attributes
* vinculo : **string** `Required`, default=`'departamento'`
* descricao : **string**, default=`'change_password'`
* Used for restrict actions of user in some cases. For instance, when user
has he first login, he must change the password.
- Relations
* Has Many : **Orgao** as `secretarioId`
* Has Many : **Sala** as `ownerId`
- ACLs
* All permissions not specified is `DENY`
* **admin** ROLE have all EXECUTE, READ and WRITE operations
......@@ -227,4 +226,17 @@
* **admin** ROLE have all EXECUTE, READ and WRITE operations
- Relations
* Belongs To : **Departamento** as `departamentoCod`
* Has and Belongs To Many : **Turma**
\ No newline at end of file
* Has and Belongs To Many : **Turma**
## Usuario
- Public: `True`
- Attributes `
* nome : **string** `Required`
* restricao : **string**, default=`'change_password'`
* Used for restrict actions of user in some cases. For instance, when user
has he first login, he must change the password.
- ACLs
* All permissions not specified is `DENY`
* **admin** ROLE have all EXECUTE, READ and WRITE operations
- Relations
* Embeds One : **Secretario** as `secretario`
......@@ -60,6 +60,12 @@
"principalId": "admin",
"permission": "ALLOW",
"property": "*"
},
{
"principalType": "ROLE",
"principalId": "secretario_setor",
"permission": "ALLOW",
"property": "*"
}
],
"methods": {}
......
......@@ -55,6 +55,12 @@
"principalId": "admin",
"permission": "ALLOW",
"property": "*"
},
{
"principalType": "ROLE",
"principalId": "comissao",
"permission": "ALLOW",
"property": "*"
}
],
"methods": {}
......
......@@ -51,6 +51,12 @@
"principalId": "admin",
"permission": "ALLOW",
"property": "*"
},
{
"principalType": "ROLE",
"principalId": "comissao",
"permission": "ALLOW",
"property": "*"
}
],
"methods": {}
......
......@@ -91,6 +91,7 @@
],
"methods": {
"prototype.addEquivalencia": {
"description": "add description",
"accepts": [
{
"arg": "disciplina_eq_code",
......@@ -98,15 +99,16 @@
}
],
"returns": {
"arg": "data",
"type": "string"
"type":"object",
"root": true
},
"http": {
"verb": "post",
"path": "/addEquivalencia"
"path": "/equivalencias"
}
},
"prototype.getEquivalencias": {
"description": "add description",
"accepts": [],
"returns": {
"arg": "data",
......@@ -114,10 +116,11 @@
},
"http": {
"verb": "get",
"path": "/getEquivalencias"
"path": "/equivalencias"
}
},
"prototype.deleteEquivalencia": {
"description": "add description",
"accepts": [
{
"arg": "disciplina_eq_code",
......@@ -125,12 +128,12 @@
}
],
"returns": {
"arg": "data",
"type": "string"
"type":"object",
"root": true
},
"http": {
"verb": "post",
"path": "/deleteEquivalencia"
"verb": "delete",
"path": "/equivalencias"
}
}
}
......
......@@ -3,7 +3,8 @@
Models are used in a lot of script, so we created this file to unify.
**/
var models = [
'User',
'Usuario',
'Secretario',
'AccessToken',
'ACL',
'RoleMapping',
......@@ -19,7 +20,6 @@ var models = [
'DisciplinaRecursodesala',
'Tipodesala',
'Orgao',
'Secretario',
'Setor',
'Departamento',
'Curso',
......
......@@ -9,8 +9,8 @@
"properties": {
"codigo": {
"type": "string",
"required": true,
"id": true
"id": true,
"required": true
},
"nome": {
"type": "string",
......
......@@ -93,6 +93,7 @@
"principalId": "$owner",
"permission": "ALLOW"
}
],
"methods": {}
}
{
"name": "Secretario",
"plural": "secretarios",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
......@@ -11,10 +10,6 @@
"type": "string",
"required": true,
"default": "departamento"
},
"restricao": {
"type": "string",
"default": "change_password"
}
},
"validations": [],
......
......@@ -57,7 +57,13 @@
"principalId": "admin",
"permission": "ALLOW",
"property": "*"
}
},
{
"principalType": "ROLE",
"principalId": "comissao",
"permission": "ALLOW",
"property": "*"
}
],
"methods": {}
}
......@@ -51,6 +51,12 @@
"principalId": "admin",
"permission": "ALLOW",
"property": "*"
},
{
"principalType": "ROLE",
"principalId": "secretario_dpto",
"permission": "ALLOW",
"property": "*"
}
],
"methods": {}
......
'use strict';
var app = require('../../server/server');
module.exports = function(Usuario) {
Usuario.prototype.setRole = function(role_name, cb) {
let usuario = this;
app.models.Role.findOne({where:{name: role_name}}, function(err, role){
if(err){
return cb(err,null);
}
if(role ===null){
var error = new Error('Role '+ role_name +' does not exists');
error.statusCode = "404";
error.code = "MODEL_NOT_FOUND";
return cb(error,null)
}
usuario.getRole(function(err,actual_role){
if(err){
return cb(err,null);
}
if(actual_role===null ){
role.principals.create({
principalType: app.models.RoleMapping.USER,
principalId: usuario.id
}, function(error, principal) {
cb(error,principal);
});
}else{
var error = new Error( "The `Usuario` instance is not valid. Details: Usuario already has a Role (value: \""+actual_role+"\");");
error.statusCode = "422";
error.name = "ValidationError";
error.code = "ValidationError";
return cb(error,null)
}
});
});
};
Usuario.prototype.getRole = function(cb) {
app.models.RoleMapping.findOne({where:{principalId: this.id.toString()}}, function(err, rolemapping){
if(err || rolemapping ===null){
return cb(err,null);
}
app.models.Role.findOne({where:{id: rolemapping.roleId}}, function(error, role){
return cb(error,role.name);
});
});
};
Usuario.prototype.deleteRole = function(cb) {
app.models.RoleMapping.destroyAll({principalId: this.id.toString()}, function(err, counter){
return cb(err,counter);
});
};
Usuario.prototype.updateRole = function(role_name,cb) {
let usuario = this;
app.models.Role.findOne({where:{name: role_name}}, function(err, role){
if(err){
return cb(err,null);
}
if(role ===null){
var error = new Error('Role '+ role_name +' does not exists');
error.statusCode = "404";
error.code = "MODEL_NOT_FOUND";
return cb(error,null)
}
app.models.RoleMapping.findOne({where:{principalId: usuario.id.toString()}}, function(err, rolemapping){
if(err){
return cb(err,null);
}
if(rolemapping===null ){
role.principals.create({
principalType: app.models.RoleMapping.USER,
principalId: usuario.id
}, function(error, principal) {
cb(error,principal);
});
}else{
rolemapping.updateAttribute('roleId',role.id,function(error,data){
cb(error,data);
});
}
});
});
};
};
{
"name": "Usuario",
"plural": "usuarios",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"nome": {
"type": "string",
"required": true
},
"restricao": {
"type": "string",
"default": "change_password"
}
},
"validations": [],
"relations": {
"secretario": {
"type": "embedsOne",
"model": "Secretario",
"property": "secretario"
}
},
"acls": [
{
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY",
"property": "*"
},
{
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW",
"property": "*"
}
],
"methods": {
"prototype.setRole": {
"description": "Assign a role to a Usuario",
"accepts": [
{
"arg": "role_name",
"type": "string",
"required": true
}
],
"returns": {
"type":"object",
"root": true
},
"http": {
"verb": "post",
"path": "/role"
}
},
"prototype.getRole": {
"description": "Get the role Assigned to a Usuario",
"accepts": [],
"returns": {
"arg": "role",
"type": "string"
},
"http": {
"verb": "get",
"path": "/role"
}
},
"prototype.updateRole": {
"description": "Replace an existing model instance or insert a new one into the data source",
"accepts": [
{
"arg": "role_name",
"type": "string",
"required": true
}
],
"returns": {
"type":"object",
"root": true
},
"http": {
"verb": "put",
"path": "/role"
}
},
"prototype.deleteRole": {
"description": "Delete a model instance by {{id}} from the data source.",
"accepts": [],
"returns": {
"type":"object",
"root": true
},
"http": {
"verb": "delete",
"path": "/role"
}
}
}
}
......@@ -13,10 +13,6 @@
"./mixins"
]
},
"User": {
"dataSource": "ensalamento",
"public": false
},
"AccessToken": {
"dataSource": "ensalamento",
"public": false
......@@ -74,7 +70,7 @@
},
"Secretario": {
"dataSource": "ensalamento",
"public": true
"public": false
},
"Setor": {
"dataSource": "ensalamento",
......@@ -91,5 +87,9 @@
"Professor": {
"dataSource": "ensalamento",
"public": true
},
"Usuario": {
"dataSource": "ensalamento",
"public": true
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment