Skip to content
This repository has been archived by the owner on Sep 8, 2021. It is now read-only.

Commit

Permalink
Improved login with status.
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed May 3, 2016
1 parent b23a5f1 commit bd57041
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {CommunityCreateComponent} from './dspace/components/community-create.com
<li><a (click)="login.openLoginModal()" class="clickable"><span class="glyphicon glyphicon-log-in space-right"></span>{{ 'header.login' | translate }}</a></li>
</ul>
<ul class="nav navbar-nav navbar-right" *ngIf="user">
<li><a [routerLink]="['/Home']"><span class="glyphicon glyphicon-user space-right"></span>{{ user.email }}</a></li>
<li><a [routerLink]="['/Home']"><span class="glyphicon glyphicon-user space-right"></span>{{ user.fullname }}</a></li>
<li><a (click)="logout()" class="clickable"><span class="glyphicon glyphicon-log-out space-right"></span>{{ 'header.logout' | translate }}</a></li>
</ul>
</div>
Expand Down
14 changes: 11 additions & 3 deletions src/app/dspace/authorization/components/login-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,17 @@ export class LoginFormComponent {
if(action == ModalAction.CONFIRM) {
this.authorization.login(this.email, this.password).subscribe(response => {
if(response.status == 200) {
this.login.hide();
this.login.finished();
this.unauthorized = false;
let token = response.text();

this.authorization.status(token).subscribe(response => {
this.login.hide();
this.login.finished();
this.unauthorized = false;
},
error => {
this.unauthorized = true;
this.login.finished();
});
}
},
error => {
Expand Down
42 changes: 29 additions & 13 deletions src/app/dspace/authorization/services/authorization.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,40 @@ export class AuthorizationService {
*/
login(email: string, password: string): Observable<Response> {

let observableResponse: Observable<Response> = this.dspace.login(email, password);
let loginResponse: Observable<Response> = this.dspace.login(email, password);

observableResponse.subscribe(response => {
loginResponse.subscribe(response => {
if(response.status == 200) {
let token = response.text();
this.user = new User(email, token);

//{
// this.storageService.store('email', email);
// this.storageService.store('token', token);
//}

}
},
error => {
console.log(error);
});

return observableResponse;
return loginResponse;
}

status(token: string): Observable<Response> {

let statusResponse: Observable<Response> = this.dspace.status(token);

statusResponse.subscribe(response => {
console.log(response);
this.user = new User(response);

console.log(this.user);

//{
// this.storageService.store('email', email);
// this.storageService.store('token', token);
//}
},
error => {
console.log(error);
});

return statusResponse;
}

/**
Expand All @@ -87,9 +103,9 @@ export class AuthorizationService {

let token = this.user.token;

let observableResponse: Observable<Response> = this.dspace.logout(token);
let logoutResponse: Observable<Response> = this.dspace.logout(token);

observableResponse.subscribe(response => {
logoutResponse.subscribe(response => {
if(response.status == 200) {
this.user = null;

Expand All @@ -103,7 +119,7 @@ export class AuthorizationService {
console.log(error);
});

return observableResponse;
return logoutResponse;
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/app/dspace/components/item-create.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {Metadatum} from '../models/metadatum.model';
</span>
</fieldset>
<hr/>
<label>Metadata</label>
<fieldset class="form-group">
<table class="table table-striped">
Expand All @@ -53,14 +55,14 @@ import {Metadatum} from '../models/metadatum.model';
<tr *ngFor="#input of metadatumInputs">
<td>
<div class="row">
<div class="col-xs-11">
<div class="col-md-11 col-xs-10">
<label>{{input.gloss}}</label>
<span class="text-danger" *ngIf="input.validation.required">*required</span>
<label class="pull-right">{{input.key}}</label>
</div>
</div>
<div class="row">
<div class="col-xs-11">
<div class="col-md-11 col-xs-10">
<input *ngIf="input.type == 'TEXT'" class="form-control" type="text" id="{{input.key}}" [(ngModel)]="input.value" ngControl="{{input.key}}">
Expand Down Expand Up @@ -102,7 +104,7 @@ import {Metadatum} from '../models/metadatum.model';
<div class="pull-right">
<button type="button" class="btn btn-default btn-sm" (click)="reset()">Reset</button>
<button type="submit" class="btn btn-primary btn-sm" [disabled]="!form.valid">Submit</button>
<button type="submit" class="btn btn-primary btn-sm" [disabled]="!name.valid && !form.valid">Submit</button>
</div>
</form>
Expand Down Expand Up @@ -157,11 +159,9 @@ export class ItemCreateComponent {
else {
validators.push(Validators[key](input.validation[key]));
}

}

formControls[input.key] = new Control('', Validators.compose(validators));

}

this.form = this.builder.group(formControls);
Expand Down Expand Up @@ -198,6 +198,8 @@ export class ItemCreateComponent {

let currentContext = this.contextProvider.context;

this.item.metadata = new Array<Metadatum>();

for(let input of this.metadatumInputs) {
if(input.value) {
this.item.metadata.push(new Metadatum(input));
Expand Down
3 changes: 0 additions & 3 deletions src/app/dspace/dspace.directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ export class DSpaceDirectory {
else if(context.type == 'collection') {
context.numberItems++;
}
else {
console.log(context.type + " does not contain count of items");
}
if(context.parentCommunity) {
this.incrementItemCount(context.parentCommunity);
}
Expand Down
2 changes: 0 additions & 2 deletions src/app/dspace/models/collection.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ export class Collection extends DSOContainer {
parentCommunity : Community;



license: string; // TODO: probably should have a license object


/**
* Create a new Collection
*
Expand Down
1 change: 0 additions & 1 deletion src/app/dspace/models/dspaceobject.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export abstract class DSpaceObject extends Pageable {
*/
constructor(json?: any) {
super(json);
this.metadata = new Array<Metadatum>();
if (ObjectUtil.isNotEmpty(json)) {
this.id = json.id;
this.name = json.name;
Expand Down
26 changes: 23 additions & 3 deletions src/app/dspace/models/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
*/
export class User {

/**
*
*/
private _fullname: string;

/**
*
*/
Expand All @@ -17,9 +22,24 @@ export class User {
/**
*
*/
constructor(email: string, token: string) {
this.email = email;
this.token = token;
constructor(json: any) {
this.fullname = json.fullname ? json.fullname : null;
this.email = json.email ? json.email : null;
this.token = json.token ? json.token : null;
}

/**
*
*/
set fullname(fullname: string) {
this._fullname = fullname;
}

/**
*
*/
get fullname(): string {
return this._fullname;
}

/**
Expand Down
24 changes: 16 additions & 8 deletions src/app/dspace/services/dspace.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,22 @@ export class DSpaceService {
});
}

/**
* Method to get user status.
*
* @param token
* DSpace user token
*/
status(token): Observable<Response> {
console.log(token);
return this.httpService.get({
url: URLHelper.relativeToAbsoluteRESTURL('/status'),
headers: [{
key: 'rest-dspace-token', value: token
}]
});
}

/**
* Method to logout.
*
Expand All @@ -195,9 +211,7 @@ export class DSpaceService {
}

createCommunity(community: Community, token: string, parentCommunityId?: string): Observable<Response> {

let path = parentCommunityId ? '/communities/' + parentCommunityId + '/communities' : '/communities';

return this.httpService.post({
url: URLHelper.relativeToAbsoluteRESTURL(path),
headers: [{
Expand All @@ -208,9 +222,7 @@ export class DSpaceService {
}

createCollection(collection: Collection, token: string, parentCommunityId: string): Observable<Response> {

let path = '/communities/' + parentCommunityId + '/collections';

return this.httpService.post({
url: URLHelper.relativeToAbsoluteRESTURL(path),
headers: [{
Expand All @@ -221,11 +233,7 @@ export class DSpaceService {
}

createItem(item: Item, token: string, parentCollectionId: string): Observable<Response> {

console.log(item);

let path = '/collections/' + parentCollectionId + '/items';

return this.httpService.post({
url: URLHelper.relativeToAbsoluteRESTURL(path),
headers: [{
Expand Down
49 changes: 22 additions & 27 deletions src/app/utilities/services/http.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,39 @@ export class HttpService {
/**
* Convinience method to instantiate and assign headers.
*
* @param hdrsArr
* an array of objects, {key: string, value: string}, containing headers
* @param request
* an object, {uri: string, data: Object}, used to GET or POST
*/
buildHeaders(hdrsArr) {
buildHeaders(request: any): Headers {
let headers = new Headers();
hdrsArr.forEach((header) => {

let headerArray = [
{ key: 'Content-Type', value: 'application/json' },
{ key: 'Accept', value: 'application/json' }
];

if(request.headers) {
headerArray = headerArray.concat(request.headers);
}

headerArray.forEach((header) => {
headers.append(header.key, header.value);
});

return headers;
}

/**
* Method to make a http POST request.
*
* @param request
* an object, {uri: string, data: Object}, used to POST
* an object, {uri: string, data: Object, headers: Array}, used to POST
*/
post(request) {
//console.log(request)
post(request: any): any {

let body = JSON.stringify(request.data);

let headerArray = [
{ key: 'Content-Type', value: 'application/json' },
{ key: 'Accept', value: 'application/json' }
];

if(request.headers) {
headerArray = headerArray.concat(request.headers);
}

let headers = this.buildHeaders(headerArray);
let headers = this.buildHeaders(request);

let options = new RequestOptions({ headers: headers });

Expand All @@ -59,24 +60,18 @@ export class HttpService {
* to a json object.
*
* @param request
* an object, {url: string}, used to GET
* an object, {url: string, headers: Array}, used to GET
*/
get(request) {
//console.log(request);
get(request: any): any {

let headers = this.buildHeaders([
{ key: 'Content-Type', value: 'application/json' },
{ key: 'Accept', value: 'application/json' }
]);
let headers = this.buildHeaders(request);

let options = new RequestOptions({
method: RequestMethod.Get,
url: request.url,
headers: headers,
search: request.search
});

return this.http.request(new Request(options)).map(response => {
return this.http.get(request.url, options).map(response => {
return response.json();
});
}
Expand Down

0 comments on commit bd57041

Please sign in to comment.