Permalink
Browse files

feat(): only user with permissions can restart or stop job/build

  • Loading branch information...
Izak88 committed Aug 21, 2017
1 parent 9b702fb commit 25e958b8012a46d246a25e0a72332b3f91125ada
@@ -4,16 +4,21 @@ import { getLastRun } from './job';
export function getBuilds(limit: number, offset: number, userId?: number): Promise<any> {
return new Promise((resolve, reject) => {
new Build()
.query(q => q.orderBy('id', 'DESC').offset(offset).limit(limit))
.fetchAll({ withRelated: [{'repository.permissions': (query) => {
if (userId) {
query.where('permissions.users_id', userId)
.andWhere('permissions.permission', true)
.orWhere('private', false);
}
}
},
'jobs.runs']})
.query(q => {
if (userId) {
q.innerJoin('repositories', 'repositories.id', 'builds.repositories_id')
.innerJoin('permissions', 'permissions.repositories_id', 'repositories.id')
.where('permissions.users_id', userId)
.andWhere('permissions.permission', true)
.orWhere('repositories.private', false)
.orderBy('id', 'DESC')
.offset(offset)
.limit(limit);
} else {
q.orderBy('id', 'DESC').offset(offset).limit(limit);
}
})
.fetchAll({ withRelated: ['repository.permissions', 'jobs.runs' ]})
.then(builds => {
if (!builds) {
reject();
@@ -30,6 +35,12 @@ export function getBuilds(limit: number, offset: number, userId?: number): Promi

return job;
});
build.hasPermission = false;
if (build.reposiotory
&& build.repository.permissions
&& build.repository.permissions[0].permission) {
build.hasPermission = true;
}

return build;
});
@@ -53,7 +64,8 @@ export function getBuild(id: number, userId?: number): Promise<any> {
},
'repository.access_token',
'jobs.runs',
'runs.job_runs']})
'runs.job_runs']
})
.then(build => {
if (!build) {
reject();
@@ -88,6 +100,12 @@ export function getBuild(id: number, userId?: number): Promise<any> {
} else {
build.repository.access_token = null;
}
build.hasPermission = false;
if (build.reposiotory
&& build.repository.permissions
&& build.repository.permissions[0].permission) {
build.hasPermission = true;
}

return build;
})
@@ -113,13 +131,18 @@ export function getBuild(id: number, userId?: number): Promise<any> {
});
}

export function getLastBuild(): Promise<any> {
export function getLastBuild(userId?: number): Promise<any> {
return new Promise((resolve, reject) => {
new Build().query(qb => {
qb.orderBy('id', 'desc');
qb.limit(1);
})
.fetch({ withRelated: ['repository', 'jobs.runs'] })
new Build().query(q => q.orderBy('id', 'desc'))
.fetch({ withRelated: [{'repository.permissions': (query) => {
if (userId) {
query.where('permissions.users_id', userId)
.andWhere('permissions.permission', true)
.orWhere('private', false);
}
}
},
'jobs.runs']})
.then(build => {
if (!build) {
reject(build);
@@ -135,6 +158,12 @@ export function getLastBuild(): Promise<any> {

return job;
});
build.hasPermission = false;
if (build.reposiotory
&& build.repository.permissions
&& build.repository.permissions[0].permission) {
build.hasPermission = true;
}

resolve(build);
});
@@ -17,8 +17,17 @@ export function getJob(jobId: number, userId?: number): Promise<any> {
if (!job) {
reject();
}
job = job.toJSON();
job.hasPermission = false;
if (job.build
&& job.build.reposiotory
&& job.build.repository.permissions
&& job.build.repository.permissions[0].permission) {
job.hasPermission = true;
}
job.hasPermission = true;

return job.toJSON();
return job;
})
.then(job => {
getBuild(job.builds_id)
@@ -118,7 +118,7 @@ export function getRepositoryByBuildId(buildId: number): Promise<any> {
});
}

export function getRepositories(userId: string, keyword: string): Promise<any[]> {
export function getRepositories(keyword: string, userId?: string): Promise<any[]> {
return new Promise((resolve, reject) => {
new Repository().query(qb => {
if (keyword !== '') {
@@ -50,22 +50,41 @@ export function webRoutes(): express.Router {
export function buildRoutes(): express.Router {
const router = express.Router();

router.get('/limit/:limit/offset/:offset', (req: express.Request, res: express.Response) => {
getBuilds(req.params.limit, req.params.offset, req.query.userId).then(builds => {
return res.status(200).json({ data: builds });
});
router.get('/limit/:limit/offset/:offset/:userid?',
(req: express.Request, res: express.Response) => {
if (req.params.userid) {
getBuilds(req.params.limit, req.params.offset, req.params.userid).then(builds => {
return res.status(200).json({ data: builds });
});
} else {
getBuilds(req.params.limit, req.params.offset).then(builds => {
return res.status(200).json({ data: builds });
});
}
});

router.get('/last', (req: express.Request, res: express.Response) => {
getLastBuild()
.then(build => res.status(200).json({ data: build }))
.catch(err => res.status(200).json({ err: err }));
router.get('/last/:userid?', (req: express.Request, res: express.Response) => {
if (req.params.userid) {
getLastBuild(req.params.userid)
.then(build => res.status(200).json({ data: build }))
.catch(err => res.status(200).json({ err: err }));
} else {
getLastBuild()
.then(build => res.status(200).json({ data: build }))
.catch(err => res.status(200).json({ err: err }));
}
});

router.get('/:id', (req: express.Request, res: express.Response) => {
getBuild(req.params.id, req.query.userId).then(build => {
return res.status(200).json({ data: build });
});
router.get('/:id/:userid?', (req: express.Request, res: express.Response) => {
if (req.params.userid) {
getBuild(req.params.id, req.params.userId).then(build => {
return res.status(200).json({ data: build });
});
} else {
getBuild(req.params.id).then(build => {
return res.status(200).json({ data: build });
});
}
});

return router;
@@ -74,10 +93,16 @@ export function buildRoutes(): express.Router {
export function jobRoutes(): express.Router {
const router = express.Router();

router.get('/:id', (req: express.Request, res: express.Response) => {
getJob(req.params.id, req.query.userId).then(job => {
return res.status(200).json({ data: job });
});
router.get('/:id/:userid?', (req: express.Request, res: express.Response) => {
if (req.params.userid) {
getJob(req.params.id, req.params.userid).then(job => {
return res.status(200).json({ data: job });
});
} else {
getJob(req.params.id, req.params.userid).then(job => {
return res.status(200).json({ data: job });
});
}
});

return router;
@@ -172,12 +197,18 @@ export function tokenRoutes(): express.Router {
export function repositoryRoutes(): express.Router {
const router = express.Router();

router.get('/', (req: express.Request, res: express.Response) => {
router.get('/:userid?', (req: express.Request, res: express.Response) => {
checkApiRequestAuth(req)
.then(() => {
getRepositories(req.query.userId, req.query.keyword).then(repos => {
return res.status(200).json({ data: repos });
}).catch(err => res.status(200).json({ status: false }));
if (req.params.userid) {
getRepositories(req.query.keyword, req.params.userid).then(repos => {
return res.status(200).json({ data: repos });
}).catch(err => res.status(200).json({ status: false }));
} else {
getRepositories(req.query.keyword).then(repos => {
return res.status(200).json({ data: repos });
}).catch(err => res.status(200).json({ status: false }));
}
}).catch(err => res.status(401).json({ data: 'Not Authorized' }));
});

@@ -24,7 +24,7 @@ <h1 class="bold">
</span>
</h1>
</div>
<div class="column is-2">
<div class="column is-2" *ngIf="build?.hasPermission">
<button class="button is-fullwidth dark" name="restart-build" type="button" (click)="restartBuild($event, build.id)" [disabled]="processingBuild">
<div class="centered">
<span class="icon">
@@ -34,7 +34,7 @@ <h1 class="bold">
</div>
</button>
</div>
<div class="column is-2">
<div class="column is-2" *ngIf="build?.hasPermission">
<button class="button is-fullwidth dark" name="stop-build" type="button" (click)="stopBuild($event, build.id)" [disabled]="processingBuild">
<div class="centered">
<span class="icon">
@@ -121,7 +121,7 @@ <h1 class="bold">
<div class="column is-1">
<span class="job-time">{{ job.time }}</span>
</div>
<div class="column is-1">
<div class="column is-1" *ngIf="build?.hasPermission">
<span class="icon" name="restart-job" (click)="restartJob($event, job.id)" [class.disabled]="job.processing">
<svg width="15px" height="14px" viewBox="0 0 15 14" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
@@ -29,7 +29,7 @@
<div class="column is-1">
<span class="build-time">{{ build?.totalTime }}</span>
</div>
<div class="column is-1 justify-end">
<div class="column is-1 justify-end" *ngIf="build?.hasPermission">
<span class="icon restart-build" (click)="restartBuild($event, build.id)" [class.disabled]="processingRequest">
<svg width="15px" height="14px" viewBox="0 0 15 14" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
@@ -180,7 +180,7 @@ export class AppBuildsComponent implements OnInit, OnDestroy {
}

fetchLastBuild(): void {
this.apiService.getLastBuild().subscribe(build => {
this.apiService.getLastBuild(this.userData.id).subscribe(build => {
if (!this.builds) {
this.builds = [];
}
@@ -24,7 +24,7 @@ <h1 class="bold">
</span>
</h1>
</div>
<div class="column is-5 justify-end right-buttons-top">
<div class="column is-5 justify-end right-buttons-top" *ngIf="job?.hasPermission">
<button class="button is-fullwidth" name="btn-restart-ssh" type="button" (click)="restartJobWithSSH($event)" [disabled]="processing">
<div class="centered">
<span class="icon">
@@ -2,6 +2,7 @@ import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { SocketService } from '../../services/socket.service';
import { ApiService } from '../../services/api.service';
import { AuthService } from '../../services/auth.service';
import { ConfigService } from '../../services/config.service';
import { Subscription } from 'rxjs/Subscription';
import { format, distanceInWordsToNow } from 'date-fns';
@@ -32,12 +33,14 @@ export class AppRepositoryComponent implements OnInit, OnDestroy {
offset: number;
updateInterval: any;
hideMoreButton: boolean;
userData: any;

constructor(
private route: ActivatedRoute,
private router: Router,
private socketService: SocketService,
private api: ApiService,
private authService: AuthService,
private config: ConfigService
) {
this.loading = true;
@@ -47,6 +50,7 @@ export class AppRepositoryComponent implements OnInit, OnDestroy {
}

ngOnInit() {
this.userData = this.authService.getData();
this.tab = 'builds';
this.url = this.config.url;

@@ -144,7 +148,7 @@ export class AppRepositoryComponent implements OnInit, OnDestroy {
}

fetchLastBuild(): void {
this.api.getLastBuild().subscribe(build => {
this.api.getLastBuild(this.userData.id).subscribe(build => {
if (!this.repo.builds) {
this.repo.builds = [];
}
@@ -21,31 +21,39 @@ export class ApiService {
}

getBuilds(limit: number, offset: number, userId?: string): Observable<any> {
const params = new URLSearchParams();
params.append('userId', userId);
return this.get(`${this.url}/builds/limit/${limit}/offset/${offset}`, params, true);
if (userId) {
return this.get(`${this.url}/builds/limit/${limit}/offset/${offset}/${userId}`, null, true);
}
return this.get(`${this.url}/builds/limit/${limit}/offset/${offset}`, null, true);
}

getLastBuild(): Observable<any> {
getLastBuild(userId?: string): Observable<any> {
if (userId) {
return this.get(`${this.url}/builds/last/${userId}`, null, true);
}
return this.get(`${this.url}/builds/last`, null, true);
}

getBuild(id: string, userId?: string): Observable<any> {
const params = new URLSearchParams();
params.append('userId', userId);
return this.get(`${this.url}/builds/${id}`, params, true);
if (userId) {
return this.get(`${this.url}/builds/${id}/${userId}`, null, true);
}
return this.get(`${this.url}/builds/${id}`, null, true);
}

getJob(id: number, userId?: string): Observable<any> {
const params = new URLSearchParams();
params.append('userId', userId);
return this.get(`${this.url}/jobs/${id}`, params, true);
if (userId) {
return this.get(`${this.url}/jobs/${id}/${userId}`, null, true);
}
return this.get(`${this.url}/jobs/${id}`, null, true);
}

getRepositories(userId: string, keyword: string): Observable<any> {
const params = new URLSearchParams();
params.append('userId', userId);
params.append('keyword', keyword);
if (userId) {
return this.get(`${this.url}/repositories/${userId}`, params, true);
}
return this.get(`${this.url}/repositories`, params, true);
}

0 comments on commit 25e958b

Please sign in to comment.