Commit 3dacd75c authored by Daniel Ravi Negi's avatar Daniel Ravi Negi 🤙🏼
Browse files

add custom AssetsController with DELETE support

parent 83074719
import 'package:aqueduct/managed_auth.dart';
import 'package:coupons_backend/controller/access_code_controller.dart';
import 'package:coupons_backend/controller/upload_controller.dart';
import 'package:coupons_backend/model/access_codes.dart';
import 'controller/access_code_controller.dart';
import 'controller/assets_controller.dart';
import 'controller/coupon_code_controller.dart';
import 'controller/coupon_controller.dart';
import 'controller/register_controller.dart';
import 'controller/store_controller.dart';
import 'controller/upload_controller.dart';
import 'controller/vendor_controller.dart';
import 'coupons_backend.dart';
import 'model/access_codes.dart';
import 'model/user.dart';
class CouponConfig extends Configuration {
@optionalConfiguration
String adminkey;
@optionalConfiguration
String couponkey;
DatabaseConfiguration database;
CouponConfig(String path) : super.fromFile(File(path));
}
class CouponsBackendChannel extends ApplicationChannel {
ManagedContext context;
AuthServer authServer;
CouponConfig config;
@override
Future prepare() async {
logger.onRecord.listen(
(rec) => print("$rec ${rec.error ?? ""} ${rec.stackTrace ?? ""}"));
// Set to false in production!
Controller.includeErrorDetailsInServerErrorResponses = true;
final config = CouponConfig(options.configurationFilePath);
final dataModel = ManagedDataModel.fromCurrentMirrorSystem();
final persistentStore = PostgreSQLPersistentStore.fromConnectionInfo(
config.database.username,
config.database.password,
config.database.host,
config.database.port,
config.database.databaseName);
context = ManagedContext(dataModel, persistentStore);
final delegate = RoleBasedAuthDelegate(context);
authServer = AuthServer(delegate);
}
@override
Controller get entryPoint {
final router = Router();
......@@ -48,7 +40,7 @@ class CouponsBackendChannel extends ApplicationChannel {
router
.route('/assets/*')
.link(() => Authorizer.bearer(authServer, scopes: ['admin', 'user']))
.link(() => FileController('assets'));
.link(() => AssetsController('assets'));
router
.route('/upload')
......@@ -86,23 +78,46 @@ class CouponsBackendChannel extends ApplicationChannel {
return router;
}
}
class CouponConfig extends Configuration {
CouponConfig(String path) : super.fromFile(File(path));
@override
Future prepare() async {
logger.onRecord.listen(
(rec) => print("$rec ${rec.error ?? ""} ${rec.stackTrace ?? ""}"));
@optionalConfiguration
String adminkey;
// Set to false in production!
Controller.includeErrorDetailsInServerErrorResponses = true;
final config = CouponConfig(options.configurationFilePath);
final dataModel = ManagedDataModel.fromCurrentMirrorSystem();
final persistentStore = PostgreSQLPersistentStore.fromConnectionInfo(
config.database.username,
config.database.password,
config.database.host,
config.database.port,
config.database.databaseName);
@optionalConfiguration
String couponkey;
DatabaseConfiguration database;
context = ManagedContext(dataModel, persistentStore);
final delegate = RoleBasedAuthDelegate(context);
authServer = AuthServer(delegate);
}
}
class RoleBasedAuthDelegate extends ManagedAuthDelegate<User> {
RoleBasedAuthDelegate(ManagedContext context, {int tokenLimit = 4})
: super(context, tokenLimit: tokenLimit);
@override
List<AuthScope> getAllowedScopes(covariant User user) {
if (user.accesscode.role == Role.admin) {
return [AuthScope('admin'), AuthScope('user'), AuthScope('coupon')];
} else if (user.accesscode.role == Role.coupon) {
return [AuthScope('user'), AuthScope('coupon')];
} else if (user.accesscode.role == Role.user) {
return [AuthScope('user')];
} else {
return [AuthScope('user')];
}
}
@override
Future<User> getResourceOwner(AuthServer server, String username) async {
final userQuery = Query<User>(context)
......@@ -124,17 +139,4 @@ class RoleBasedAuthDelegate extends ManagedAuthDelegate<User> {
return user;
}
@override
List<AuthScope> getAllowedScopes(covariant User user) {
if (user.accesscode.role == Role.admin) {
return [AuthScope('admin'), AuthScope('user'), AuthScope('coupon')];
} else if (user.accesscode.role == Role.coupon) {
return [AuthScope('user'), AuthScope('coupon')];
} else if (user.accesscode.role == Role.user) {
return [AuthScope('user')];
} else {
return [AuthScope('user')];
}
}
}
import 'package:coupons_backend/coupons_backend.dart';
class AssetsController extends FileController {
AssetsController(String pathOfDirectoryToServe)
: super(pathOfDirectoryToServe);
/// adding missing DELETE implementation to [FileController]
@override
Future<RequestOrResponse> handle(Request request) async {
// check for DELETE method and launch custom code accordingly
if (request.method == 'DELETE') {
final filename = request.path.remainingPath;
final file = File("$assetsFolderPath${Platform.pathSeparator}$filename");
if (!file.existsSync()) {
return Response.badRequest();
}
await file.delete();
if (!file.existsSync()) {
return Response.ok(null);
} else {
return Response.badRequest();
}
}
// if method!=DELETE go to parent implementation
return super.handle(request);
}
}
......@@ -40,7 +40,6 @@ class UploadController extends ResourceController {
@Operation.post()
Future<Response> postFile() async {
const assetsFolderPath = 'assets';
var filename = '${DateTime.now().millisecondsSinceEpoch}';
final newFilePath = '$assetsFolderPath${Platform.pathSeparator}$filename';
......
......@@ -10,3 +10,4 @@ export 'package:aqueduct/aqueduct.dart';
export 'channel.dart';
const assetsFolderPath = 'assets';
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