Commit 1795aaff authored by Nacim Goura's avatar Nacim Goura

add mapping elasticsearch for each user

parent fef1fde1
...@@ -4,6 +4,7 @@ import { check } from 'meteor/check'; ...@@ -4,6 +4,7 @@ import { check } from 'meteor/check';
import { Accounts } from 'meteor/accounts-base'; import { Accounts } from 'meteor/accounts-base';
import { Roles } from 'meteor/alanning:roles'; import { Roles } from 'meteor/alanning:roles';
import formAccountSchema from '/imports/api/account/formAccountSchema'; import formAccountSchema from '/imports/api/account/formAccountSchema';
import { defineConfig } from '/imports/api/config/methods';
/** /**
* add user account * add user account
...@@ -17,6 +18,11 @@ export function addAccount(user) { ...@@ -17,6 +18,11 @@ export function addAccount(user) {
throw new Meteor.Error('Error', 'Impossible de créer un compte!'); throw new Meteor.Error('Error', 'Impossible de créer un compte!');
} }
Roles.addUsersToRoles(id, 'gestionnaire'); Roles.addUsersToRoles(id, 'gestionnaire');
console.log(user);
defineConfig({
userId: id,
listConfig: [],
});
return user; return user;
} }
......
...@@ -31,6 +31,7 @@ SimpleSchema.configCollection = new SimpleSchema({ ...@@ -31,6 +31,7 @@ SimpleSchema.configCollection = new SimpleSchema({
type: String, type: String,
label: 'Nom de domaine', label: 'Nom de domaine',
regEx: SimpleSchema.RegEx.Url, regEx: SimpleSchema.RegEx.Url,
required: false,
autoValue() { autoValue() {
return this.value.replace('https', 'http'); return this.value.replace('https', 'http');
}, },
......
...@@ -14,7 +14,10 @@ export function defineConfig(config) { ...@@ -14,7 +14,10 @@ export function defineConfig(config) {
if (!config.listConfig) { if (!config.listConfig) {
throw new Meteor.Error('Error', 'Impossible de définir les configurations'); throw new Meteor.Error('Error', 'Impossible de définir les configurations');
} }
config.userId = Meteor.userId(); console.log(config);
if (!config.userId) {
config.userId = Meteor.userId();
}
_.forEach(config.listConfig, (item) => { _.forEach(config.listConfig, (item) => {
item.forbiddenWord = item.forbiddenWordString.split(','); item.forbiddenWord = item.forbiddenWordString.split(',');
}); });
......
...@@ -14,8 +14,9 @@ export default class crawlWebsite extends CrawlGeneric { ...@@ -14,8 +14,9 @@ export default class crawlWebsite extends CrawlGeneric {
* @param data * @param data
* @returns {Promise} * @returns {Promise}
*/ */
constructor(data) { constructor(data, contextIndex) {
super(); super();
this.contextIndex = contextIndex;
this.urlWebsite = data.urlWebsite; this.urlWebsite = data.urlWebsite;
this.config.crawl = _.find(this.config.listConfig, n => n.domain === data.nameConfig); this.config.crawl = _.find(this.config.listConfig, n => n.domain === data.nameConfig);
return this.start(); return this.start();
...@@ -75,7 +76,7 @@ export default class crawlWebsite extends CrawlGeneric { ...@@ -75,7 +76,7 @@ export default class crawlWebsite extends CrawlGeneric {
if (toQueueUrl.match(/(pdf)/)) { if (toQueueUrl.match(/(pdf)/)) {
listPdf.push(toQueueUrl); listPdf.push(toQueueUrl);
} else { } else {
console.log(toQueueUrl); // console.log(toQueueUrl);
crawl.queue(toQueueUrl); crawl.queue(toQueueUrl);
} }
} }
...@@ -85,7 +86,7 @@ export default class crawlWebsite extends CrawlGeneric { ...@@ -85,7 +86,7 @@ export default class crawlWebsite extends CrawlGeneric {
} }
}); });
// if url has not parameter => parse data // if url has not parameter => parse data
if (!res.options.uri.match(/(#.*|\?.*)/g)) { if (!/(#.*|\?.*)/g.test(res.options.uri)) {
this.parseData(res.$, res.options.uri, listPdf); this.parseData(res.$, res.options.uri, listPdf);
} }
} }
...@@ -108,7 +109,7 @@ export default class crawlWebsite extends CrawlGeneric { ...@@ -108,7 +109,7 @@ export default class crawlWebsite extends CrawlGeneric {
* @param currentUrl * @param currentUrl
* @param listPdf * @param listPdf
*/ */
parseData($, currentUrl, listPdf) { async parseData($, currentUrl, listPdf) {
const body = $('body'); const body = $('body');
body.html(checkData.cleanHtml(body.html())); body.html(checkData.cleanHtml(body.html()));
const title = checkData.cleanText($('title').text()); const title = checkData.cleanText($('title').text());
...@@ -143,7 +144,16 @@ export default class crawlWebsite extends CrawlGeneric { ...@@ -143,7 +144,16 @@ export default class crawlWebsite extends CrawlGeneric {
dataForIndex.listPdf = listPdf.join(' '); dataForIndex.listPdf = listPdf.join(' ');
} }
this.listDataForIndex.push({ const _index = Meteor.settings.private.elasticsearch.esIndex;
const _type = this.config.userId;
const _id = currentUrl;
this.listDataForIndex.push(dataForIndex);
return this.contextIndex.indexOne(_index, _type, _id, dataForIndex);
/* this.listDataForIndex.push({
index: { index: {
_index: Meteor.settings.private.elasticsearch.esIndex, _index: Meteor.settings.private.elasticsearch.esIndex,
_type: this.config.userId, _type: this.config.userId,
...@@ -152,5 +162,6 @@ export default class crawlWebsite extends CrawlGeneric { ...@@ -152,5 +162,6 @@ export default class crawlWebsite extends CrawlGeneric {
}); });
this.listDataForIndex.push(dataForIndex); this.listDataForIndex.push(dataForIndex);
*/
} }
} }
...@@ -30,7 +30,6 @@ export function indexWebsite(data) { ...@@ -30,7 +30,6 @@ export function indexWebsite(data) {
export function indexApi(data) { export function indexApi(data) {
check(data, Object); check(data, Object);
console.log(data);
formApiCrawlSchema.validate(data); formApiCrawlSchema.validate(data);
const index = new IndexApi(); const index = new IndexApi();
return index.start(data); return index.start(data);
......
...@@ -10,6 +10,11 @@ export default class IndexGeneric { ...@@ -10,6 +10,11 @@ export default class IndexGeneric {
constructor() { constructor() {
this.config = getConfig(); this.config = getConfig();
console.log(this.config);
}
indexOne(esIndex, esType, id, obj) {
return elastic.index(esIndex, esType, id, obj);
} }
indexByBulk(data, hasFile) { indexByBulk(data, hasFile) {
...@@ -35,7 +40,12 @@ export default class IndexGeneric { ...@@ -35,7 +40,12 @@ export default class IndexGeneric {
const analyser = configElastic.analyser; const analyser = configElastic.analyser;
const mapping = configElastic.mapping; const mapping = configElastic.mapping;
await elastic.initAnalyzer(esIndex, analyser); /**
* init seulement le mapping
* (chaque user à son propre type sur elastic)
* initialiser l'analyzer supprimerai egalement le mapping des autres
*/
// await elastic.initAnalyzer(esIndex, analyser);
await elastic.initMapping(esIndex, this.config.userId, mapping); await elastic.initMapping(esIndex, this.config.userId, mapping);
return elastic.createPipeline(); return elastic.createPipeline();
} }
......
...@@ -11,14 +11,14 @@ export default class IndexWebsite extends IndexGeneric { ...@@ -11,14 +11,14 @@ export default class IndexWebsite extends IndexGeneric {
*/ */
async start(data) { async start(data) {
try { try {
const dataToIndex = await new CrawlWebsite(data); const dataToIndex = await new CrawlWebsite(data, this);
/** /**
* dataToIndex index website page * dataToIndex index website page
* false no file * false no file
*/ */
const resultIndex = await this.indexByBulk(dataToIndex, false); // const resultIndex = await this.indexByBulk(dataToIndex, false);
return { return {
message: `Indexation de ${resultIndex.items.length} liens pour ${data.urlWebsite} fini avec succès!`, message: `Indexation de ${dataToIndex.length} liens pour ${data.urlWebsite} fini avec succès!`,
}; };
} catch (error) { } catch (error) {
throw error; throw error;
......
...@@ -26,6 +26,14 @@ SimpleSchema.notifsCollection = new SimpleSchema({ ...@@ -26,6 +26,14 @@ SimpleSchema.notifsCollection = new SimpleSchema({
}, },
userId: { userId: {
type: String, type: String,
autoValue() {
if (this.userId) {
return this.userId;
} else if (this.value) {
return this.value;
}
return 'non défini!';
},
}, },
}); });
......
...@@ -5,10 +5,10 @@ import _ from 'lodash'; ...@@ -5,10 +5,10 @@ import _ from 'lodash';
import Search from '/imports/api/search/server/search'; import Search from '/imports/api/search/server/search';
import formSearchSchema from '/imports/api/search/formSearchSchema'; import formSearchSchema from '/imports/api/search/formSearchSchema';
export async function searchWebsite(data) { export async function searchWebsite(data, userId) {
check(data, Object); check(data, Object);
formSearchSchema.validate(data); formSearchSchema.validate(data);
const search = new Search(); const search = new Search(userId);
try { try {
const results = await search.searchWebsite(data.searchTerm); const results = await search.searchWebsite(data.searchTerm);
return { return {
...@@ -21,9 +21,9 @@ export async function searchWebsite(data) { ...@@ -21,9 +21,9 @@ export async function searchWebsite(data) {
} }
} }
export async function searchApi(data) { export async function searchApi(data, userId) {
check(data, Object); check(data, Object);
const search = new Search(); const search = new Search(userId);
try { try {
const results = await search.searchApi(data.searchTerm); const results = await search.searchApi(data.searchTerm);
return { return {
...@@ -36,9 +36,9 @@ export async function searchApi(data) { ...@@ -36,9 +36,9 @@ export async function searchApi(data) {
} }
} }
export async function searchDocument(data) { export async function searchDocument(data, userId) {
check(data, Object); check(data, Object);
const search = new Search(); const search = new Search(userId);
try { try {
const results = await search.searchDocument(data.searchTerm); const results = await search.searchDocument(data.searchTerm);
return { return {
...@@ -52,11 +52,12 @@ export async function searchDocument(data) { ...@@ -52,11 +52,12 @@ export async function searchDocument(data) {
} }
export async function searchAll(data) { export async function searchAll(data) {
const userId = Meteor.userId();
try { try {
return { return {
website: await searchWebsite(data), website: await searchWebsite(data, userId),
api: await searchApi(data), api: await searchApi(data, userId),
document: await searchDocument(data), document: await searchDocument(data, userId),
}; };
} catch (error) { } catch (error) {
console.log(error); console.log(error);
...@@ -66,7 +67,7 @@ export async function searchAll(data) { ...@@ -66,7 +67,7 @@ export async function searchAll(data) {
export async function autoCompletion(term) { export async function autoCompletion(term) {
check(term, String); check(term, String);
const search = new Search(); const search = new Search(Meteor.userId());
try { try {
const results = await search.autoComplete(term); const results = await search.autoComplete(term);
if (results.suggest) { if (results.suggest) {
......
...@@ -2,10 +2,13 @@ ...@@ -2,10 +2,13 @@
import indexationElastic from '/imports/libs/elasticsearch/elasticsearch'; import indexationElastic from '/imports/libs/elasticsearch/elasticsearch';
const esIndex = Meteor.settings.private.elasticsearch.esIndex; const esIndex = Meteor.settings.private.elasticsearch.esIndex;
const esType = Meteor.settings.private.elasticsearch.esType;
export default class Search { export default class Search {
constructor(userId) {
this.esType = userId;
}
/** /**
* search * search
* @param term * @param term
...@@ -67,7 +70,7 @@ export default class Search { ...@@ -67,7 +70,7 @@ export default class Search {
}; };
} }
return indexationElastic.search(esIndex, esType, params); return indexationElastic.search(esIndex, this.esType, params);
} }
searchApi(term) { searchApi(term) {
...@@ -121,7 +124,7 @@ export default class Search { ...@@ -121,7 +124,7 @@ export default class Search {
}; };
} }
return indexationElastic.search(esIndex, esType, params); return indexationElastic.search(esIndex, this.esType, params);
} }
searchDocument(term) { searchDocument(term) {
...@@ -170,7 +173,7 @@ export default class Search { ...@@ -170,7 +173,7 @@ export default class Search {
}; };
} }
return indexationElastic.search(esIndex, esType, params); return indexationElastic.search(esIndex, this.esType, params);
} }
/** /**
......
...@@ -239,10 +239,10 @@ export default { ...@@ -239,10 +239,10 @@ export default {
async initMapping(esIndex, esType, obj) { async initMapping(esIndex, esType, obj) {
console.log('init mapping'); console.log('init mapping');
try { try {
const exists = await this.typeExists(esIndex, esType); /* const exists = await this.typeExists(esIndex, esType);
if (exists) { if (exists) {
await this.mappingDelete(esIndex, esType); await this.mappingDelete(esIndex, esType);
} }*/
await this.indexClose(esIndex); await this.indexClose(esIndex);
await this.mappingCreate(esIndex, esType, obj); await this.mappingCreate(esIndex, esType, obj);
return this.indexOpen(esIndex); return this.indexOpen(esIndex);
......
...@@ -32,6 +32,11 @@ adminSection.route('/account', { ...@@ -32,6 +32,11 @@ adminSection.route('/account', {
navbar: 'navbarLayoutTpl', navbar: 'navbarLayoutTpl',
}); });
}, },
triggersEnter: [(context, redirect) => {
if (Meteor.user() && Meteor.user().roles && Meteor.user().roles[0] !== 'admin') {
redirect('home');
}
}],
}); });
adminSection.route('/stat', { adminSection.route('/stat', {
...@@ -43,6 +48,11 @@ adminSection.route('/stat', { ...@@ -43,6 +48,11 @@ adminSection.route('/stat', {
navbar: 'navbarLayoutTpl', navbar: 'navbarLayoutTpl',
}); });
}, },
triggersEnter: [(context, redirect) => {
if (Meteor.user() && Meteor.user().roles && Meteor.user().roles[0] !== 'admin') {
redirect('home');
}
}],
}); });
adminSection.route('/notif', { adminSection.route('/notif', {
......
...@@ -31,10 +31,15 @@ FlowRouter.route('/', { ...@@ -31,10 +31,15 @@ FlowRouter.route('/', {
sidebar: 'sidebarLayoutTpl', sidebar: 'sidebarLayoutTpl',
}); });
}, },
triggersEnter: [(context, redirect) => {
if (!Meteor.userId()) {
redirect('login');
}
}],
}); });
FlowRouter.route('/demo', { FlowRouter.route('/demo', {
name: 'home', name: 'demo',
action() { action() {
BlazeLayout.render('mainLayoutTpl', { BlazeLayout.render('mainLayoutTpl', {
main: 'demoTpl', main: 'demoTpl',
...@@ -56,7 +61,7 @@ FlowRouter.route('/logout', { ...@@ -56,7 +61,7 @@ FlowRouter.route('/logout', {
name: 'logout', name: 'logout',
action() { action() {
Meteor.logout(); Meteor.logout();
FlowRouter.go('/'); FlowRouter.go('login');
displayNotif({ displayNotif({
type: 'info', type: 'info',
title: 'Info : ', title: 'Info : ',
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import { Meteor } from 'meteor/meteor'; import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base'; import { Accounts } from 'meteor/accounts-base';
import { Roles } from 'meteor/alanning:roles'; import { Roles } from 'meteor/alanning:roles';
import { defineConfig } from "/imports/api/config/methods";
Meteor.startup(() => { Meteor.startup(() => {
/** /**
...@@ -36,6 +37,10 @@ Meteor.startup(() => { ...@@ -36,6 +37,10 @@ Meteor.startup(() => {
if (newUser) { if (newUser) {
Roles.addUsersToRoles(newUser, 'admin'); Roles.addUsersToRoles(newUser, 'admin');
defineConfig({
userId: newUser._id,
listConfig: [],
});
} }
console.log('==================================================='); console.log('===================================================');
......
...@@ -10,12 +10,14 @@ ...@@ -10,12 +10,14 @@
<li> <li>
<a href="/admin"><i class="fa fa-fw fa-etsy fa-20px" aria-hidden="true"></i> Indexation</a> <a href="/admin"><i class="fa fa-fw fa-etsy fa-20px" aria-hidden="true"></i> Indexation</a>
</li> </li>
{{#if grantedAccessAdmin}}
<li> <li>
<a href="/admin/account"><i class="fa fa-fw fa-user fa-20px" aria-hidden="true"></i> Comptes</a> <a href="/admin/account"><i class="fa fa-fw fa-user fa-20px" aria-hidden="true"></i> Comptes</a>
</li> </li>
<li> <li>
<a href="/admin/stat"><i class="fa fa-fw fa-bar-chart-o fa-20px" aria-hidden="true"></i> Statistiques</a> <a href="/admin/stat"><i class="fa fa-fw fa-bar-chart-o fa-20px" aria-hidden="true"></i> Statistiques</a>
</li> </li>
{{/if}}
<li> <li>
<a class="" href="/logout" ><i class="fa fa-fw fa-lock fa-20px"></i> Déconnexion</a> <a class="" href="/logout" ><i class="fa fa-fw fa-lock fa-20px"></i> Déconnexion</a>
</li> </li>
......
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import './sidebar.html'; import './sidebar.html';
Template.sidebarLayoutTpl.helpers({
grantedAccessAdmin: () => !!(Meteor.user() && Meteor.user().roles && Meteor.user().roles[0] === 'admin'),
});
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