From b3291391ffec8a6063b0e40219ce89f354584d15 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 15 Dec 2020 00:29:45 +0100 Subject: [PATCH] Login/logout, Firestore rules, readme --- README.md | 45 +++++++++++++++----------- firestore.rules | 23 ++++++++++--- src/app/app.component.html | 17 ++++++---- src/app/app.component.ts | 32 ++++++++++-------- src/app/login/login.component.html | 4 +-- src/app/login/login.component.ts | 14 ++++++-- src/app/register/register.component.ts | 8 +++-- src/app/shared/login.service.ts | 24 ++++++++++++-- 8 files changed, 116 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 58d1ce4..880ba02 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,36 @@ # Szakdolgozat +Egy webalkalmazás, amely nyomonköveti egy-egy kurzus követelményeinek teljesitését oktatók és hallgatók számára. -This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 11.0.2. +## Szerepkörök +Csak bejelentkezett felhasználók férhetnek hozzá bármilyen adathoz. A saját adataikat mindig tudják módositani. -## Development server +### Admin +* Teljes jogosultsága van az adatokhoz, kivéve a felhasználók adatait +* Hozzá tud rendelni más felhasználókat szerepkörökhöz egy-egy kurzus kapcsán -Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. +### Hallgató +* Az adott kurzushoz tartozó adatokat csak megtekinteni tudja -## Code scaffolding +### Oktató +* Az adott kurzushoz tartozó összes adatot tudja módositani, kivéve a nevet, azonositót. -Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. +## Adatok +### Kurzus +* Azonositó +* Név +* Leirás +* Követelmények +* Eredmények +* Oktatók +* Hallgatók -## Build +### Követelmény +* ... -Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. +### Eredmény +* ... -## Running unit tests - -Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). - -## Running end-to-end tests - -Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/). - -## Further help - -To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page. +### Felhasználó +* Azonositó +* E-mail, jelszó (külön tárolva) +* Admin-e diff --git a/firestore.rules b/firestore.rules index 7e6c70c..e994b4b 100644 --- a/firestore.rules +++ b/firestore.rules @@ -1,9 +1,24 @@ rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { - match /{document=**} { - allow read, write: if - request.time < timestamp.date(2021, 1, 10); + function sameUser(user) { + return request.auth != null && request.auth.uid == user; + } + function getUserData() { + return get(/databases/$(database)/documents/users/$(request.auth.uid)).data; + } + //Felhasználói adatok kezelése + match /users/{user} { + allow create: if sameUser(user) && request.auth.uid == request.resource.data.author_uid; + allow get, list, update, delete: if sameUser(user) && request.auth.uid == resource.data.author_uid; + } + //Adminoknak mindent lehet + match /data/{document=**} { + allow get, list, create, update, delete: if getUserData().isAdmin == true; + } + //Diákok megnézhetik a kurzus adatait + match /data/courses/{course} { + allow get, list: if request.auth.uid in resource.data.students; } } -} \ No newline at end of file +} diff --git a/src/app/app.component.html b/src/app/app.component.html index 3e40aa2..97b8050 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -14,7 +14,7 @@ Szakdolgozat - Regisztráció - - Bejelentkezés - login - + + Kijelentkezés + + + Regisztráció + + Bejelentkezés + login + +
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 34675d7..4249b20 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,15 +1,17 @@ -import { Component } from '@angular/core'; -import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout'; -import { Observable } from 'rxjs'; -import { map, shareReplay } from 'rxjs/operators'; +import {Component, OnInit} from '@angular/core'; +import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout'; +import {Observable} from 'rxjs'; +import {map, shareReplay} from 'rxjs/operators'; +import {LoginService} from './shared/login.service'; import firebase from 'firebase'; +import {Router} from '@angular/router'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) -export class AppComponent { +export class AppComponent implements OnInit { isHandset$: Observable = this.breakpointObserver.observe(Breakpoints.Handset) .pipe( @@ -17,13 +19,15 @@ export class AppComponent { shareReplay() ); - constructor(private breakpointObserver: BreakpointObserver) {} - -} - -firebase.initializeApp((window as any).firebaseCredentials); -firebase.auth().onAuthStateChanged(user => { - if (user) { - + constructor(private breakpointObserver: BreakpointObserver, public loginService: LoginService, + private router: Router) { } -}); + + ngOnInit(): void { + } + + async logout(): Promise { + await this.loginService.logout(); + await this.router.navigate(['/']); + } +} diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index e3397b0..9fff068 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -3,13 +3,13 @@ Email + placeholder="h123456@stud.u-szeged.hu" [(ngModel)]="email"/> Egyetemi email cim Jelszó + minlength="8" [(ngModel)]="pass"/> diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts index b64a9e8..1a1e8ad 100644 --- a/src/app/login/login.component.ts +++ b/src/app/login/login.component.ts @@ -1,4 +1,6 @@ import {Component, OnInit} from '@angular/core'; +import {Router} from '@angular/router'; +import {LoginService} from '../shared/login.service'; @Component({ selector: 'app-login', @@ -6,10 +8,18 @@ import {Component, OnInit} from '@angular/core'; styleUrls: ['./login.component.css'] }) export class LoginComponent implements OnInit { + email: string; + pass: string; + + constructor(private router: Router, private loginService: LoginService) { + } + + ngOnInit(): void { } - doLogin(): void { - + async doLogin(): Promise { + await this.loginService.login(this.email, this.pass); + await this.router.navigate(['/']); } } diff --git a/src/app/register/register.component.ts b/src/app/register/register.component.ts index f73ba5d..209323b 100644 --- a/src/app/register/register.component.ts +++ b/src/app/register/register.component.ts @@ -2,6 +2,8 @@ import {Component, OnInit} from '@angular/core'; import {ErrorStateMatcher} from '@angular/material/core'; import {AbstractControl, FormControl, FormGroupDirective, NgForm, ValidationErrors, Validators} from '@angular/forms'; import firebase from 'firebase'; +import {LoginService} from '../shared/login.service'; +import {Router} from '@angular/router'; export class FormErrorStateMatcher implements ErrorStateMatcher { isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean { @@ -17,7 +19,7 @@ export class FormErrorStateMatcher implements ErrorStateMatcher { }) export class RegisterComponent implements OnInit { - constructor() { + constructor(private loginService: LoginService, private router: Router) { } emailFormControl = new FormControl('', [ @@ -73,8 +75,8 @@ export class RegisterComponent implements OnInit { return; } try { - const user = await firebase.auth().createUserWithEmailAndPassword(this.emailFormControl.value, this.passFormControl.value); - alert('User created: ' + user); + await this.loginService.createUser(this.emailFormControl.value, this.passFormControl.value); + await this.router.navigate(['/']); } catch (e) { alert(e); } diff --git a/src/app/shared/login.service.ts b/src/app/shared/login.service.ts index 92c777e..1e8818f 100644 --- a/src/app/shared/login.service.ts +++ b/src/app/shared/login.service.ts @@ -1,9 +1,29 @@ -import { Injectable } from '@angular/core'; +import {Injectable} from '@angular/core'; +import firebase from 'firebase'; @Injectable({ providedIn: 'root' }) export class LoginService { - constructor() { } + loggedInUser: firebase.User; + + constructor() { + firebase.initializeApp((window as any).firebaseCredentials); + firebase.auth().onAuthStateChanged(user => { + this.loggedInUser = user; + }); + } + + async createUser(email: string, pass: string): Promise { + const user = await firebase.auth().createUserWithEmailAndPassword(email, pass); + } + + async logout(): Promise { + await firebase.auth().signOut(); + } + + async login(email: string, pass: string): Promise { + await firebase.auth().signInWithEmailAndPassword(email, pass); + } }