Skip to content

Commit

Permalink
feat (api, cli): display groups on user settings (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored and sguiheux committed Jun 19, 2017
1 parent b0f999e commit 9297c73
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 26 deletions.
6 changes: 1 addition & 5 deletions engine/api/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/go-gorp/gorp"

"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/log"
)

// DeleteGroupAndDependencies deletes group and all subsequent group_project, pipeline_project
Expand Down Expand Up @@ -58,14 +57,11 @@ func AddGroup(db gorp.SqlExecutor, group *sdk.Group) (int64, bool, error) {
defer rows.Close()

if rows.Next() {
log.Warning("AddGroup: Group %s already exists\n", group.Name)

var groupID int64
if err := rows.Scan(&groupID); err != nil {
return 0, false, sdk.WrapError(sdk.ErrGroupExists, "AddGroup: Cannot get the ID of the existing group %s (%s)", group.Name, err)
}

return groupID, false, sdk.ErrGroupExists
return groupID, false, sdk.WrapError(sdk.ErrGroupExists, "AddGroup: Group %s already exists")
}

if err := InsertGroup(db, group); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion engine/api/main_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ func (router *Router) init() {
// Users
router.Handle("/user", GET(GetUsers))
router.Handle("/user/signup", Auth(false), POST(AddUser))
router.Handle("/user/group", Auth(true), GET(getUserGroupsHandler))
router.Handle("/user/import", NeedAdmin(true), POST(importUsersHandler))
router.Handle("/user/{username}", NeedUsernameOrAdmin(true), GET(GetUserHandler), PUT(UpdateUserHandler), DELETE(DeleteUserHandler))
router.Handle("/user/{username}/groups", NeedUsernameOrAdmin(true), GET(getUserGroupsHandler))
router.Handle("/user/{username}/confirm/{token}", Auth(false), GET(ConfirmUser))
router.Handle("/user/{username}/reset", Auth(false), POST(ResetUser))
router.Handle("/auth/mode", Auth(false), GET(AuthModeHandler))
Expand Down
39 changes: 19 additions & 20 deletions engine/api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,29 @@ func GetUserHandler(w http.ResponseWriter, r *http.Request, db *gorp.DbMap, c *b

// getUserGroupsHandler returns groups of the user
func getUserGroupsHandler(w http.ResponseWriter, r *http.Request, db *gorp.DbMap, c *businesscontext.Ctx) error {
log.Debug("getUserGroupsHandler> get groups for user %d", c.User.ID)
vars := mux.Vars(r)
username := vars["username"]

var groups, groupsAdmin []sdk.Group
if !c.User.Admin && username != c.User.Username {
return WriteJSON(w, r, nil, http.StatusForbidden)
}

//Admin are considered as admin of all groups
if c.User.Admin {
allgroups, err := group.LoadGroups(db)
if err != nil {
return err
}
u, errl := user.LoadUserWithoutAuth(db, username)
if errl != nil {
return sdk.WrapError(errl, "getUserHandler: Cannot load user from db")
}

groups = allgroups
groupsAdmin = allgroups
} else {
var err1, err2 error
groups, err1 = group.LoadGroupByUser(db, c.User.ID)
if err1 != nil {
return err1
}
var groups, groupsAdmin []sdk.Group

groupsAdmin, err2 = group.LoadGroupByAdmin(db, c.User.ID)
if err2 != nil {
return err2
}
var err1, err2 error
groups, err1 = group.LoadGroupByUser(db, u.ID)
if err1 != nil {
return sdk.WrapError(err1, "getUserGroupsHandler: Cannot load group by user")
}

groupsAdmin, err2 = group.LoadGroupByAdmin(db, u.ID)
if err2 != nil {
return sdk.WrapError(err2, "getUserGroupsHandler: Cannot load group by admin")
}

res := map[string][]sdk.Group{}
Expand Down
10 changes: 10 additions & 0 deletions ui/src/app/model/group.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ export class Group {
}
}

export class Groups {
groups: Array<Group>;
groups_admin: Array<Group>;

constructor() {
this.groups = [];
this.groups_admin = [];
}
}

export class GroupPermission {
group: Group;
permission: number;
Expand Down
9 changes: 9 additions & 0 deletions ui/src/app/service/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Injectable} from '@angular/core';
import {Http, Response, RequestOptions, Headers} from '@angular/http';
import {User} from '../../model/user.model';
import {Groups} from '../../model/group.model';
import {Observable} from 'rxjs/Rx';
import {AuthentificationStore} from '../auth/authentification.store';

Expand Down Expand Up @@ -89,6 +90,14 @@ export class UserService {
return this._http.get('/user').map(res => res.json());
}

/**
* Get user groups.
* @returns {Observable<User[]>}
*/
getGroups(username: string): Observable<Groups> {
return this._http.get('/user/' + username + '/groups').map(res => res.json());
}

/**
* Get a user by his username
* @param username username of the user to get
Expand Down
20 changes: 20 additions & 0 deletions ui/src/app/views/settings/user/edit/user.edit.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {User} from '../../../../model/user.model';
import {Group} from '../../../../model/group.model';
import {UserService} from '../../../../service/user/user.service';
import {ToastService} from '../../../../shared/toast/ToastService';
import {TranslateService} from 'ng2-translate';
Expand All @@ -16,6 +17,8 @@ export class UserEditComponent implements OnInit {
deleteLoading = false;
user: User;
currentUser: User;
groups: Array<Group>;
groupsAdmin: Array<Group>;

private username: string;
private usernamePattern: RegExp = new RegExp('^[a-zA-Z0-9._-]{1,}$');
Expand All @@ -34,6 +37,23 @@ export class UserEditComponent implements OnInit {
this._userService.getUser(this.username).subscribe( u => {
this.user = u;
this.username = this.user.username;
this.groups = [];

this._userService.getGroups(this.user.username).subscribe( g => {
this.groupsAdmin = g.groups_admin;
for (let i = 0; i < g.groups.length; i++) {
let userAdminOnGroup = false;
for (let j = 0; j < this.groupsAdmin.length; j++) {
if (this.groupsAdmin[j].name === g.groups[i].name) {
userAdminOnGroup = true;
break;
}
}
if (!userAdminOnGroup) {
this.groups.push(g.groups[i]);
}
}
});
});
});
}
Expand Down
27 changes: 27 additions & 0 deletions ui/src/app/views/settings/user/edit/user.edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,33 @@ <h2 *ngIf="user && user.id && user.username !== currentUser.username">{{ 'user_e
<i class="save icon"></i>{{ 'btn_save' | translate }}
</button>
</div>

<h3>{{ 'group_list_title' | translate }}</h3>
<table class="ui fixed celled table" *ngIf="groups || groupsAdmin">
<thead>
<tr>
<th class="three wide">{{ 'group_name' | translate }}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let v of groupsAdmin">
<td>
<a class="ui" [routerLink]="['/settings', 'group', v?.name]">
<i class="fa fa-user-circle-o" title="{{ 'group_user_is_admin' | translate }}"></i>
{{v?.name}}
</a>
</td>
</tr>
<tr *ngFor="let v of groups">
<td>
<a class="ui" [routerLink]="['/settings', 'group', v?.name]">
{{v?.name}}
</a>
</td>
</tr>
</tbody>
</table>

</div>

<div class="six wide column">
Expand Down

0 comments on commit 9297c73

Please sign in to comment.