Commit 409fc1ce authored by Open Source Developer's avatar Open Source Developer

currentuser

parent 8f3ed10c
...@@ -23,13 +23,13 @@ epal_auth_test: ...@@ -23,13 +23,13 @@ epal_auth_test:
_permission: 'access content' _permission: 'access content'
_user_is_logged_in: 'TRUE' _user_is_logged_in: 'TRUE'
current_user: current_user:
path: '/epal/curuser/{token_name}' path: '/epal/curuser'
options: options:
_auth: [ 'basic_auth' ] _auth: [ 'basic_auth' ]
defaults: defaults:
_controller: '\Drupal\epal\Controller\CurrentUser::content' _controller: '\Drupal\epal\Controller\CurrentUser::content'
requirements: requirements:
_access: 'TRUE' _user_is_logged_in: 'TRUE'
epal.application_submit: epal.application_submit:
path: '/epal/appsubmit' path: '/epal/appsubmit'
options: options:
......
<?php <?php
/**
* @file
* Contains \Drupal\query_example\Controller\QueryExampleController.
*/
namespace Drupal\epal\Controller; namespace Drupal\epal\Controller;
...@@ -15,26 +7,54 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; ...@@ -15,26 +7,54 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class CurrentUser extends ControllerBase { class CurrentUser extends ControllerBase {
protected $entityTypeManager;
public function content($token_name) {
public function __construct(EntityTypeManagerInterface $entityTypeManager )
$query = \Drupal::database()->select('epal_users', 'nfd'); {
$query->fields('nfd', ['name']); $this->entityTypeManager1 = $entityTypeManager;
$query->condition('nfd.authtoken', $token_name);
$field = $query->execute()->fetchAssoc(); }
public static function create(ContainerInterface $container) {
return new static(
$response = new JsonResponse($field); $container->get('entity_type.manager'),
return $response; $container->get('entity.query'),
$container->get('entity_field.manager')
} );
}
public function content(Request $request) {
$authToken = $request->headers->get('PHP_AUTH_USER');
$epalUsers = $this->entityTypeManager1->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
$epalUser = reset($epalUsers);
if ($epalUser)
{
$currentUserName = $epalUser->name->value;
$response = new JsonResponse(['name' =>$currentUserName]);
}
else {
$response = new Response();
$response->setContent('forbidden');
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->headers->set('Content-Type', 'application/json');
}
return $response;
}
} }
...@@ -90,7 +90,8 @@ export default class Home implements OnInit { ...@@ -90,7 +90,8 @@ export default class Home implements OnInit {
state.loginInfo.reduce(({}, loginInfoToken) => { state.loginInfo.reduce(({}, loginInfoToken) => {
this.authToken = loginInfoToken.auth_token; this.authToken = loginInfoToken.auth_token;
this.authRole = loginInfoToken.auth_role; this.authRole = loginInfoToken.auth_role;
if (this.authToken && this.authToken.length > 0)
this.router.navigate(['/epal-class-select']);
return loginInfoToken; return loginInfoToken;
}, {}); }, {});
} }
...@@ -100,9 +101,6 @@ export default class Home implements OnInit { ...@@ -100,9 +101,6 @@ export default class Home implements OnInit {
// subscribe to router event // subscribe to router event
this.activatedRoute.queryParams.subscribe((params: Params) => { this.activatedRoute.queryParams.subscribe((params: Params) => {
this.authToken = params['auth_token'];
this.authRole = params['auth_role'];
if (params) { if (params) {
this.authToken = params['auth_token']; this.authToken = params['auth_token'];
this.authRole = params['auth_role']; this.authRole = params['auth_role'];
...@@ -110,10 +108,6 @@ export default class Home implements OnInit { ...@@ -110,10 +108,6 @@ export default class Home implements OnInit {
if (this.authToken && this.authRole) if (this.authToken && this.authRole)
this._ata.getloginInfo({ auth_token: this.authToken, auth_role: this.authRole}); this._ata.getloginInfo({ auth_token: this.authToken, auth_role: this.authRole});
console.log(this.authToken, "tttttttt");
if (this.authToken && this.authToken.length > 0)
this.router.navigate(['/epal-class-select']);
// this._ata.saveLoginInfo({ auth_token: this.authToken, auth_role: this.authRole, cu_name:this.name });
}); });
} }
......
<div class="nav-wrapper"> <div class="nav-wrapper" class="content">
<div class="container"> <div class="container">
<nav class="navbar navbar-toggleable-sm"> <nav class="navbar navbar-toggleable-sm">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
......
...@@ -233,8 +233,23 @@ export class HelperDataService { ...@@ -233,8 +233,23 @@ export class HelperDataService {
getCurrentUser(oauthtoken, oauthrole) { getCurrentUser(oauthtoken, oauthrole) {
this.authToken = oauthtoken;
let headers = new Headers({
//"Authorization": "Basic cmVzdHVzZXI6czNjckV0MFAwdWwwJA==", // encoded user:pass
// "Authorization": "Basic bmthdHNhb3Vub3M6emVtcmFpbWU=",
"Content-Type": "application/json",
// "Content-Type": "text/plain", // try to skip preflight
//"X-CSRF-Token": "hVtACDJjFRSyE4bgGJENHbXY0B9yNhF71Fw-cYHSDNY"
//"X-CSRF-Token": "fj1QtF_Z_p6kE19EdCnN08zoSjVfcT4Up-ciW6I0IG8"
"X-CSRF-Token": "LU92FaWYfImfZxfldkF5eVnssdHoV7Aa9fg8K1bWYUc",
// "X-oauth-enabled": "true",
// "X-Auth-Token": this.authToken
});
this.createAuthorizationHeader(headers);
let options = new RequestOptions({ headers: headers });
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.http.get(`${AppSettings.API_ENDPOINT}/epal/curuser/${oauthtoken} `) this.http.get(`${AppSettings.API_ENDPOINT}/epal/curuser`, options)
.map(response => response.json()) .map(response => response.json())
.subscribe(data => { .subscribe(data => {
resolve(this.transformUserSchema(data, oauthtoken, oauthrole)); resolve(this.transformUserSchema(data, oauthtoken, oauthrole));
...@@ -256,7 +271,7 @@ transformUserSchema(userlogin:any,oauthtoken:string, oauthrole:string){ ...@@ -256,7 +271,7 @@ transformUserSchema(userlogin:any,oauthtoken:string, oauthrole:string){
} }
signOut() { signOut() {
this.loginInfo$.forEach(loginInfoToken => { this.loginInfo$.forEach(loginInfoToken => {
console.log(loginInfoToken.get(0)); console.log(loginInfoToken.get(0));
this.authToken = loginInfoToken.get(0).auth_token; this.authToken = loginInfoToken.get(0).auth_token;
......
...@@ -13,7 +13,7 @@ export function loginInfoReducer(state: ILoginInfo = INITIAL_STATE, action): ILo ...@@ -13,7 +13,7 @@ export function loginInfoReducer(state: ILoginInfo = INITIAL_STATE, action): ILo
let loginInfoTokens = Array<ILoginInfoToken>(); let loginInfoTokens = Array<ILoginInfoToken>();
let i=0; let i=0;
action.payload.loginInfos.forEach(loginInfo => { action.payload.loginInfos.forEach(loginInfo => {
loginInfoTokens.push(<ILoginInfoToken>{auth_token: loginInfo.auth_token, auth_role: loginInfo.auth_role, cu_name: loginInfo.cu_name, xcsrftoken: loginInfo.xcsrftoken }); loginInfoTokens.push(<ILoginInfoToken>{auth_token: loginInfo.auth_token, auth_role: loginInfo.auth_role, cu_name: loginInfo.cu_name});
i++; i++;
}); });
return Seq(loginInfoTokens).map(n => n).toList(); return Seq(loginInfoTokens).map(n => n).toList();
......
...@@ -3,7 +3,7 @@ import { ILoginInfo, ILoginInfoToken } from './logininfo.types'; ...@@ -3,7 +3,7 @@ import { ILoginInfo, ILoginInfoToken } from './logininfo.types';
export function deimmutifyLoginInfo(state: ILoginInfo): ILoginInfoToken[] { export function deimmutifyLoginInfo(state: ILoginInfo): ILoginInfoToken[] {
let fetchedLoginInfoTokens = new Array(); let fetchedLoginInfoTokens = new Array();
state.forEach(loginInfoToken => { state.forEach(loginInfoToken => {
fetchedLoginInfoTokens.push(<ILoginInfoToken>{auth_token: loginInfoToken.auth_token, auth_role: loginInfoToken.auth_role, cu_name: loginInfoToken.cu_name, xcsrftoken: loginInfoToken.xcsrftoken}); fetchedLoginInfoTokens.push(<ILoginInfoToken>{auth_token: loginInfoToken.auth_token, auth_role: loginInfoToken.auth_role, cu_name: loginInfoToken.cu_name});
}); });
return fetchedLoginInfoTokens; return fetchedLoginInfoTokens;
}; };
...@@ -4,7 +4,6 @@ export interface ILoginInfoToken { ...@@ -4,7 +4,6 @@ export interface ILoginInfoToken {
auth_token: string; auth_token: string;
auth_role: string; auth_role: string;
cu_name: string; cu_name: string;
xcsrftoken:string;
} }
export type ILoginInfo = List<ILoginInfoToken>; export type ILoginInfo = List<ILoginInfoToken>;
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