Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gate class,update web socket #131

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/src/authentication/gate/gate.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Gate {
static final Gate _instance = Gate._internal();

factory Gate() {
return _instance;
}

Gate._internal();

/// All of the defined abilities.
final Map<String, Function> _abilities = {};

/// Define a new ability.
/// Gate().define('canDeletePost', () {
/// return user.id == post.user_id;
/// });
void define(String ability, Function callback) {
_abilities[ability] = callback;
}

/// Determine if all of the given abilities should be granted for the current user.
/// Gate().allows('canDeletePost');
bool allows(String ability) {
final gate = _abilities[ability];
if (gate != null) {
return gate();
}
return false;
}

/// Determine if a given ability has been defined.
bool has(String ability) {
return _abilities.containsKey(ability);
}

/// Determine if any of the given abilities should be denied for the current user.
bool denies(String ability) {
return !allows(ability);
}
}
2 changes: 2 additions & 0 deletions lib/src/http/request/request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Request {

Request.from({required this.request, this.route});

Map? get user => Auth().user();

String? get ip => request.connectionInfo?.remoteAddress.address;

HttpHeaders get _httpHeaders => request.headers;
Expand Down
4 changes: 4 additions & 0 deletions lib/src/utils/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ String url(String path) => '${env<String>('APP_URL')}/$path';

String assets(String src) => url(src);

bool can(String ability) => Gate().allows(ability);

bool cannot(String ability) => Gate().denies(ability);

T env<T>(String key, [dynamic defaultValue]) => Env.get<T>(key, defaultValue);

abort(int code, String message) {
Expand Down
15 changes: 15 additions & 0 deletions lib/src/websocket/web_socket_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class WebSocketHandler implements WebSocketEvent {
},
}));

Function? openFunction = _events['${routePath}_connect'];
if (openFunction != null) {
Function.apply(openFunction, <dynamic>[client]);
}

websocket.listen((data) async {
try {
if (_middleware[_websocketRoute] != null) {
Expand Down Expand Up @@ -107,8 +112,18 @@ class WebSocketHandler implements WebSocketEvent {
}
Function.apply(controller, <dynamic>[client, message]);
}, onDone: () {
Function? openFunction = _events['${routePath}_disconnect'];
if (openFunction != null) {
Function.apply(openFunction, <dynamic>[client]);
}

_session.removeSession(sessionId);
}, onError: (_) {
Function? openFunction = _events['${routePath}_error'];
if (openFunction != null) {
Function.apply(openFunction, <dynamic>[client]);
}

_session.removeSession(sessionId);
});
}
Expand Down
2 changes: 2 additions & 0 deletions lib/vania.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ export 'src/redis/vania_redis.dart';
export 'src/cache/redis_cache_driver.dart';

export 'src/http/validation/validation_chain/export_chain_validation.dart';

export 'src/authentication/gate/gate.dart';
Loading