Commit c08a79b9 authored by HellLab's avatar HellLab
Browse files

Merge remote-tracking branch 'origin/AqueductBranch' into AqueductBranch

# Conflicts:
#	frontend/lib/src/model/appointment.dart
#	frontend/lib/src/view/services/person_service.dart
parents 58e3b9e8 b4f74daf
......@@ -9,7 +9,7 @@ class UserService {
UserService(this._http);
static final _headers = {'Content-Type': 'application/json'};
static const _userUrl = host+'/user'; // URL to web API
static const _userUrl = host + '/user'; // URL to web API
final Client _http;
///Liest die Daten aus einer Response
......@@ -26,21 +26,22 @@ class UserService {
final response = await _http.post(_userUrl,
headers: _headers, body: json.encode(user.toJson()));
return User.fromJson(
_extractData(response) as Map<String, dynamic>);
_extractData(response as Response) as Map<String, dynamic>);
} catch (e) {
throw _handleError(e);
}
}
Future<List<User>> getAll() async {
final Response response = await _http.get('$_userUrl');
final Response response = await _http.get('$_userUrl') as Response;
return (_extractData(response) as List)
.map((value) => User.fromJson(value as Map<String, dynamic>))
.toList();
}
Future<User> get(String contactCode) async {
final Response response = await _http.get('$_userUrl/$contactCode');
final Response response =
await _http.get('$_userUrl/$contactCode') as Response;
return User.fromJson(_extractData(response) as Map<String, dynamic>);
}
......
import 'dart:async';
import 'dart:convert';
import 'package:demo/src/view/main_component.dart';
import 'package:http/http.dart';
import 'package:demo/src/model/person.dart';
import 'dart:html';
class RegisterService {
// request to middlemand
RegisterService(this._http);
static final _headers = {'Content-Type': 'application/json'};
static const _userUrl = host+'/register'; // URL to web API
final Client _http;
///Liest die Daten aus einer Response
dynamic _extractData(Response resp) => json.decode(resp.body);
Exception _handleError(dynamic e) {
print(e); // for demo purposes only
return Exception('Server error; cause: $e');
}
///Erstellt einen neuen User mit gegebenem Namen, Email und Passwort
Future<User> create(User user) async {
try {
final response = await _http.post(_userUrl,
headers: _headers, body: json.encode(user.toJson()));
return User.fromJson(
_extractData(response as Response) as Map<String, dynamic>);
} catch (e) {
throw _handleError(e);
}
}
Future login(String username, String password) async {
// Must include http package in your pubspec.yaml
const clientID = "com.calendar.app";
final body = "username=$username&password=$password&grant_type=password";
// Note the trailing colon (:) after the clientID.
// A client identifier secret would follow this, but there is no secret, so it is the empty string.
final clientCredentials = Base64Encoder().convert("$clientID:".codeUnits);
final response = await _http.post("$host/auth/token",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic $clientCredentials"
},
body: body);
final Map<String,dynamic> jsonMap = json.decode(response.body as String) as Map<String, dynamic>;
capitalize(String s) => s[0].toUpperCase() + s.substring(1);
print(jsonMap.toString());
window.localStorage.addAll({
"access_token": jsonMap["access_token"] as String,
"token_type": capitalize(jsonMap["token_type"] as String),
"expires_in": (jsonMap["expires_in"] as int).toString()
});
}
}
......@@ -18,4 +18,3 @@ dependencies:
build_web_compilers: ^1.0.0
pedantic: ^1.0.0
analyzer: ^0.34.3
test: ^1.0.0
\ No newline at end of file
......@@ -15,8 +15,8 @@ import 'main.template.dart' as self;
// Import 'package:http/browser_client.dlart' and change the above to:
// ClassProvider(Client, useClass: BrowserClient),
])
InjectorFactory injector = self.injector$Injector as InjectorFactory;
InjectorFactory injector = self.injector$Injector;
void main() {
runApp(ng.AppComponentNgFactory as ComponentFactory, createInjector: injector);
runApp(ng.AppComponentNgFactory, createInjector: injector);
}
database:
host: postgresql
port: 5432
username: dart
password: dart
databaseName: dart_test
\ No newline at end of file
host: $CAL_DB_HOST
port: $CAL_DB_PORT
username: $CAL_DB_USER
password: $CAL_DB_PASS
databaseName: $CAL_DB_NAME
\ No newline at end of file
host: localhost
port: 5432
username: cal_user
password: password
databaseName: calendar
\ No newline at end of file
host: $CAL_DB_HOST
port: $CAL_DB_PORT
username: $CAL_DB_USER
password: $CAL_DB_PASS
databaseName: $CAL_DB_NAME
\ No newline at end of file
import 'package:aqueduct/managed_auth.dart';
import 'package:middleman/controller/appointment_controller.dart';
import 'package:middleman/controller/contact_controller.dart';
import 'package:middleman/controller/register_controller.dart';
import 'package:middleman/controller/user_controller.dart';
import 'package:middleman/model/person.dart';
import 'middleman.dart';
......@@ -10,6 +13,7 @@ import 'middleman.dart';
/// database connections. See http://aqueduct.io/docs/http/channel/.
class CalenderChannel extends ApplicationChannel {
ManagedContext context;
AuthServer authServer;
/// Initialize services in this method.
///
......@@ -19,6 +23,8 @@ class CalenderChannel extends ApplicationChannel {
/// This method is invoked prior to [entryPoint] being accessed.
@override
Future prepare() async {
logger.onRecord.listen(
(rec) => print("$rec ${rec.error ?? ""} ${rec.stackTrace ?? ""}"));
......@@ -32,6 +38,10 @@ class CalenderChannel extends ApplicationChannel {
config.database.databaseName);
context = ManagedContext(dataModel, persistentStore);
// AuthStorage
final authStorage = ManagedAuthDelegate<User>(context);
authServer = AuthServer(authStorage);
}
/// Construct the request channel.
......@@ -43,12 +53,21 @@ class CalenderChannel extends ApplicationChannel {
@override
Controller get entryPoint {
final router = Router();
// registerStuff
router
.route('/register')
.link(() => RegisterController(context, authServer));
// add this route
router
.route('/auth/token')
.link(() => AuthController(authServer));
router
.route('/appointments/[:id]')
.link(() => Authorizer.bearer(authServer))
.link(() => AppointmentController(context));
router
.route('/appointments/lookup/[:year/[:month/[:day]]]')
.link(() => Authorizer.bearer(authServer))
.link(() => AppointmentController(context));
router
.route('/user/[:number]')
......@@ -56,6 +75,7 @@ class CalenderChannel extends ApplicationChannel {
router
.route('/contacts/[:number]')
.link(() => ContactController(context));
return router;
}
}
......
......@@ -18,6 +18,7 @@ class AppointmentController extends ResourceController {
// getall
@Operation.get()
Future<Response> getAllAppointments() async {
logger.warning(request.body.toString());
final appointmentQuery = Query<Appointment>(context);
final appointments = await appointmentQuery.fetch();
if (appointments == null) {
......@@ -66,6 +67,7 @@ class AppointmentController extends ResourceController {
@Operation.post()
Future<Response> newAppointment() async {
logger.warning(request.body.toString());
final Map<String, dynamic> body = await request.body.decode();
final query = Query<Appointment>(context)..values.read(body,ignore: ["id"]);
final insertedApp = await query.insert();
......@@ -76,6 +78,7 @@ class AppointmentController extends ResourceController {
@Operation.put()
Future<Response> updateAppointment() async {
final Map<String, dynamic> body = await request.body.decode();
logger.warning(request.body.decode());
final query = Query<Appointment>(context)
..values.read(body,ignore: ["id"])
..where((app) => app.id).equalTo(body["id"] as int);
......
import 'dart:async';
import 'dart:async';
import 'package:aqueduct/aqueduct.dart';
import 'package:middleman/model/person.dart';
......
import 'dart:async';
import 'dart:convert';
import 'package:aqueduct/aqueduct.dart';
import 'package:middleman/model/person.dart';
import 'package:http/http.dart' as http; // Must include http package in your pubspec.yaml
class RegisterController extends ResourceController {
RegisterController(this.context, this.authServer);
final ManagedContext context;
final AuthServer authServer;
@Operation.post()
Future<Response> createUser(@Bind.body() User user) async {
// Check for required parameters before we spend time hashing
if (user.username == null || user.password == null ){
return Response.badRequest(
body: {"error": "username and password required."});
}
logger.warning("hier");
logger.warning(user.id);
user.salt = AuthUtility.generateRandomSalt();
user.hashedPassword = authServer.hashPassword(user.password, user.salt);
return Response.ok(await Query(context, values: user).insert());
}
}
......@@ -37,6 +37,32 @@ class UserController extends ResourceController {
return Response.ok(user);
}
@Operation.get('password', 'username')
Future<Response> getUserLoginNickname(@Bind.path('password') String password,
@Bind.path('username') String username) async {
final userQuery = Query<User>(context)
..where((user) => user.hashedPassword).equalTo(password)
..where((user) => user.username).equalTo(username);
final user = await userQuery.fetchOne();
if (user == null) {
return Response.notFound();
}
return Response.ok(user);
}
@Operation.get('password', 'email')
Future<Response> getUserLoginEmail(@Bind.path('password') String password,
@Bind.path('email') String email) async {
final userQuery = Query<User>(context)
..where((user) => user.hashedPassword).equalTo(password)
..where((user) => user.email).equalTo(email);
final user = await userQuery.fetchOne();
if (user == null) {
return Response.notFound();
}
return Response.ok(user);
}
@Operation.post()
Future<Response> newUser() async {
final Map<String, dynamic> body = await request.body.decode();
......
import 'package:aqueduct/managed_auth.dart';
import 'package:middleman/middleman.dart';
class Person extends ManagedObject<_Person> implements _Person {}
class User extends ManagedObject<_User> implements _User {}
class User extends ManagedObject<_User> implements _User,ManagedAuthResourceOwner<_User> {
@Serialize(input: true, output: false)
String password;
}
class Contact extends ManagedObject<_Contact> implements _Contact {}
abstract class _Person {
class _Contact{
@Column(unique: false,nullable: true)
String note;
@primaryKey
int id;
@Column(unique: true, nullable: true)
String contactCode;
@Column(unique: true)
String nickname;
@Column(unique: false)
String surname;
......@@ -26,15 +31,22 @@ abstract class _Person {
String email;
}
class _Contact extends _Person {
@Column(unique: false,nullable: true)
String note;
}
class _User extends _Person {
@Column(unique: false)
String password;
class _User extends ResourceOwnerTableDefinition {
@Column(unique: false, nullable: true)
String mobileNo;
@Column(unique: true, nullable: true)
String contactCode;
@Column(unique: false)
String surname;
@Column(unique: false)
String name;
@Column(unique: true)
String email;
}
......@@ -4,328 +4,19 @@ import 'package:aqueduct/aqueduct.dart';
class Migration1 extends Migration {
@override
Future upgrade() async {
database.createTable(SchemaTable("_Appointment", [
SchemaColumn("id", ManagedPropertyType.bigInteger,
isPrimaryKey: true,
autoincrement: true,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("name", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("time", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("year", ManagedPropertyType.integer,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("month", ManagedPropertyType.integer,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("day", ManagedPropertyType.integer,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("duration", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("location", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("note", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false)
]));
database.createTable(SchemaTable("_Contact", [
SchemaColumn("note", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: true,
isUnique: false),
SchemaColumn("id", ManagedPropertyType.bigInteger,
isPrimaryKey: true,
autoincrement: true,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("contactCode", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: true,
isUnique: true),
SchemaColumn("nickname", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: true),
SchemaColumn("surname", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("name", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("email", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: true)
]));
database.createTable(SchemaTable("_User", [
SchemaColumn("password", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("mobileNo", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: true,
isUnique: false),
SchemaColumn("id", ManagedPropertyType.bigInteger,
isPrimaryKey: true,
autoincrement: true,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("contactCode", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: true,
isUnique: true),
SchemaColumn("nickname", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: true),
SchemaColumn("surname", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("name", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("email", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: true)
]));
database.createTable(SchemaTable("_Person", [
SchemaColumn("id", ManagedPropertyType.bigInteger,
isPrimaryKey: true,
autoincrement: true,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("contactCode", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: true,
isUnique: true),
SchemaColumn("nickname", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: true),
SchemaColumn("surname", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("name", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: false),
SchemaColumn("email", ManagedPropertyType.string,
isPrimaryKey: false,
autoincrement: false,
isIndexed: false,
isNullable: false,
isUnique: true)
]));
database.createTable(SchemaTable("_authclient", [SchemaColumn("id", ManagedPropertyType.string, isPrimaryKey: true, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("hashedSecret", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: true, isUnique: false),SchemaColumn("salt", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: true, isUnique: false),SchemaColumn("redirectURI", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: true, isUnique: false),SchemaColumn("allowedScope", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: true, isUnique: false)]));
database.createTable(SchemaTable("_authtoken", [SchemaColumn("id", ManagedPropertyType.bigInteger, isPrimaryKey: true, autoincrement: true, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("code", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: true, isNullable: true, isUnique: true),SchemaColumn("accessToken", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: true, isNullable: true, isUnique: true),SchemaColumn("refreshToken", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: true, isNullable: true, isUnique: true),SchemaColumn("scope", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: true, isUnique: false),SchemaColumn("issueDate", ManagedPropertyType.datetime, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("expirationDate", ManagedPropertyType.datetime, isPrimaryKey: false, autoincrement: false, isIndexed: true, isNullable: false, isUnique: false),SchemaColumn("type", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: true, isNullable: true, isUnique: false)]));
database.createTable(SchemaTable("_Contact", [SchemaColumn("note", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: true, isUnique: false),SchemaColumn("id", ManagedPropertyType.bigInteger, isPrimaryKey: true, autoincrement: true, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("contactCode", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: true, isUnique: true),SchemaColumn("surname", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("name", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("email", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: true)]));
database.createTable(SchemaTable("_User", [SchemaColumn("mobileNo", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: true, isUnique: false),SchemaColumn("contactCode", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: true, isUnique: true),SchemaColumn("surname", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("name", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("email", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: true),SchemaColumn("id", ManagedPropertyType.bigInteger, isPrimaryKey: true, autoincrement: true, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("username", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: true, isNullable: false, isUnique: true),SchemaColumn("hashedPassword", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("salt", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false)]));
database.createTable(SchemaTable("_Appointment", [SchemaColumn("id", ManagedPropertyType.bigInteger, isPrimaryKey: true, autoincrement: true, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("name", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("time", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("year", ManagedPropertyType.integer, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("month", ManagedPropertyType.integer, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("day", ManagedPropertyType.integer, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("duration", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("location", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false),SchemaColumn("note", ManagedPropertyType.string, isPrimaryKey: false, autoincrement: false, isIndexed: false, isNullable: false, isUnique: false)]));
database.addColumn("_authtoken", SchemaColumn.relationship("resourceOwner", ManagedPropertyType.bigInteger, relatedTableName: "_User", relatedColumnName: "id", rule: DeleteRule.cascade, isNullable: false, isUnique: false));
database.addColumn("_authtoken", SchemaColumn.relationship("client", ManagedPropertyType.string, relatedTableName: "_authclient", relatedColumnName: "id", rule: DeleteRule.cascade, isNullable: false, isUnique: false));
}
@override
Future downgrade() async {}
@override
Future seed() async {
final _appointments = [
{
"id": 1,
"name": "eins",
"year": 2019,
"month": 7,
"day": 22,
"time": "12:00",
"duration": "04:00",
"location": "Technischehochschule Lübeck",
"note": ""
},
{
"id": 2,