Managing Permissions With EF Core Migrations | Permission Authorization - Part 2

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ม.ค. 2025

ความคิดเห็น •

  • @MilanJovanovicTech
    @MilanJovanovicTech  2 ปีที่แล้ว +3

    Get the source code for this video for FREE → the-dotnet-weekly.ck.page/permissions2
    Want to master Clean Architecture? Go here: bit.ly/3PupkOJ
    Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt

    • @TaureanKing83
      @TaureanKing83 2 ปีที่แล้ว

      Great content. Keep them coming. It would also be great to see a video for resource-based authorization.

  • @nouchance
    @nouchance 2 ปีที่แล้ว +3

    Thank you Milan✊💯🤝 From Uzbekistan!

  • @subashbarik
    @subashbarik 2 ปีที่แล้ว +2

    Awesome , waiting for the next video of this series.

  • @dunderball716
    @dunderball716 2 ปีที่แล้ว +1

    Awesome series, bro! Keep it up! Greetings from Bulgaria 🇧🇬!

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว +1

      The next video will be the most important, it's where we stick it all together

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว

      Благодаря много 😁

  • @danielle8624
    @danielle8624 ปีที่แล้ว +4

    What's inside the Enumeration class? (in Gatherly.Domain.Primitives) Is there any video that covers this?

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว +1

      Take a look at this video: th-cam.com/video/v6cYTcEfZ8A/w-d-xo.html

  • @laolaoal4784
    @laolaoal4784 ปีที่แล้ว +1

    what a clean way to implement that!
    pretty interesting. thank u for this tutorial

  • @Corsair4Fun
    @Corsair4Fun 2 ปีที่แล้ว +1

    Keep up the good work Milan.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว +1

      Thank you!

    • @Corsair4Fun
      @Corsair4Fun 2 ปีที่แล้ว

      @@MilanJovanovicTech I also wanted to recommend a similiar library, but YT keeps removing my comments for some reason.

  • @fernandocalmet
    @fernandocalmet 2 ปีที่แล้ว +1

    As always your content is the best

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว

      Thanks, Fernando, I think you're one of the best supporters I have 😊

  • @benij
    @benij 3 หลายเดือนก่อน +1

    Amazing way to implement that Milan! From Perú 💯!

  • @Mazur_Family_Travel
    @Mazur_Family_Travel 3 หลายเดือนก่อน +1

    Awesome! Liked, subscribed)
    Keep continuing to create such intensive and helpful lessons)

  • @ramytawfik9168
    @ramytawfik9168 ปีที่แล้ว +1

    Great as always Milan 👌

  • @jwcodendaal
    @jwcodendaal ปีที่แล้ว +1

    Beautiful implementation. Top job.

  • @MaxSupercars
    @MaxSupercars 2 ปีที่แล้ว +1

    Great ideas! Thank you! Looking forward for continuation! 😊

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว

      Thank you, glad you liked it. The next video will put everything together 😁

    • @MaxSupercars
      @MaxSupercars 2 ปีที่แล้ว +1

      @@MilanJovanovicTech Great thank you! I like that you split this serie in more videos, it's easier to watch. It only got maybe disadvantage for new subscribers, that it's not all in 1 video. But it would be long and maybe less interesting to watch.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว

      @@MaxSupercars The entire authentication series is close to 90 minutes, so definitely need to split it

  • @bykaos2012
    @bykaos2012 2 ปีที่แล้ว +1

    Thank you Milan for video

  • @nove1398
    @nove1398 2 ปีที่แล้ว +1

    Another great video!

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว +1

      Thank you. Next one is the most important in the series 😁

  • @mylesdavies9476
    @mylesdavies9476 2 ปีที่แล้ว +1

    Really like the conversion of the enum to a new entity with key:value and the automatic seeding. I have always avoided use of enums with efcore for things like status values as I couldn't figure out a way to get the full key and value into the DB reliably. Will try to replicate this, thanks

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว

      You can also use value converters, and just store the enum value

    • @mylesdavies9476
      @mylesdavies9476 2 ปีที่แล้ว +1

      @@MilanJovanovicTech I've always wanted both in the DB though as it's useful to be able to see what the value means

  • @olufeming
    @olufeming ปีที่แล้ว +2

    Hi Millan, I love the you structure your code and its so clean indeed, but then the more I watch the more confused I get. Is there anyway you can help by making things perhaps understandable or probably make recommendations on how to catchup.

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว

      Start from Part 1 🤷‍♂️ And possibly code along

  • @VakhushtiMetreveli
    @VakhushtiMetreveli 2 ปีที่แล้ว +1

    I have sum issuse with this lection, The collection navigation 'Role' cannot be added to the entity type 'User' because its CLR type 'Role' does not implement 'IEnumerable'. Collection navigations must implement IEnumerable of the related entity.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว

      You probably did something different with the relationship configuration?

    • @VakhushtiMetreveli
      @VakhushtiMetreveli 2 ปีที่แล้ว

      @@MilanJovanovicTech i think no, in user config i have builder.HasOne(e => e.Role)
      .WithMany()
      .HasForeignKey("RoleID");

  • @Tamer_Ali
    @Tamer_Ali ปีที่แล้ว +1

    Hi Milan,
    I'd like to categorize the Permissions and I'll create enum for them
    public enum PermissionCategory { Admins= 1, Users= 2}
    should I loop the Permission enum and add PermissionCategory value as the foreign key
    or create a custom attribute "say Category" with each Permission then use your way to seed permissions data?
    public enum Permission
    {
    [Category(PermissionCategory.Admins)]
    ManageRoles = 1,
    [Category(PermissionCategory.Users)]
    ManageProfiles = 2
    }

  • @Tamer_Ali
    @Tamer_Ali 3 หลายเดือนก่อน

    Is it better to use HasPermission attribute with IRequest query or command implementation as Jason Taylor did with Authorize attribute in clean architecture?

    • @MilanJovanovicTech
      @MilanJovanovicTech  3 หลายเดือนก่อน

      I prefer doing auth on the endpoints

  • @E_G_
    @E_G_ 2 ปีที่แล้ว +1

    Great Video. Thank you :)

  • @paulbarton2280
    @paulbarton2280 ปีที่แล้ว +2

    How do you prevent EF from trying to create a new Role (with a duplicate key) every time you save changes?

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว +1

      Answered on Patreon also, but for anyone reading - just call Set.Attach(roleInstance) and it won't be an issue

    • @thesm9734
      @thesm9734 9 หลายเดือนก่อน

      @@MilanJovanovicTech That doesn't work when there are more Roles of the same type attached to more users while saving changes. Any clean idea on how to preload those Roles from the database ?
      For reference the error is: "The instance of entity type 'Role' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked".
      The error happens before I can even attach them (my approach was to use a save changes interceptor). It happens once the ChangeTracker gets read, as it tries to detect changes. Oh and also, I would rather not deactivate the auto detection of changes as it helps with saving changes.
      Thanks in advance for the answer!

    • @thesm9734
      @thesm9734 9 หลายเดือนก่อน

      Alternatively, I managed to create a backing field where I store only the Id for the Role inside the User class. Outside my domain I get access to the role by using a get only property that searches inside the Role.GetValues() for that Id, so when creating users, instead of letting ef core decide whether the Role is a new entity or not, I only persist the backing field Id and ignore the property altogether inside my db config. (I would still be interested in preloading data, as it may cover another use case that this example didn't)

  • @bilalmehrban
    @bilalmehrban 2 ปีที่แล้ว +1

    I have implemented this in two projects recently, one thing that I struggled with was how to make a menu out of this permission enum and only show those menu items which the user has permission to.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว +1

      You need to be able to read the permissions client side
      Either by fetching from DB, or passing them in JWT

  • @javidibra6786
    @javidibra6786 7 หลายเดือนก่อน

    Hi , First of all i wanna say that it is amazing content thanx for sharing such a great content. But there is a question that makes me think: after making our project working in production server , how we can add new permission enum? we only have to add that on code side? and after adding new permission enum we should make new migrate, how we can handle that situation with production code?

    • @MilanJovanovicTech
      @MilanJovanovicTech  7 หลายเดือนก่อน +1

      Create a new migration, and add the permissions to the DB

  • @ayedabboushi7221
    @ayedabboushi7221 ปีที่แล้ว +2

    Enumeration is missing please reorder videos on the series

  • @unimatrix20a
    @unimatrix20a 11 หลายเดือนก่อน

    i watched your video on Enumerations but I dont find a GetValues static method there. Am I missing something?

    • @MilanJovanovicTech
      @MilanJovanovicTech  11 หลายเดือนก่อน

      It just returns the dictionary values

  • @StressedProgrammer
    @StressedProgrammer 5 หลายเดือนก่อน

    whats the Enumeration class? is it some type of custom made enums?

    • @MilanJovanovicTech
      @MilanJovanovicTech  5 หลายเดือนก่อน

      Here: th-cam.com/video/v6cYTcEfZ8A/w-d-xo.html

  • @RoshdiAlMajzoub
    @RoshdiAlMajzoub 11 หลายเดือนก่อน

    I need your Help
    I am getting this error while running the application
    Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Authorization.IAuthorizeData Lifetime: Scoped ImplementationType: Gatherly.Infrastructure.Authentication.HasPermissionAttribute': Unable to resolve service for type 'Gatherly.Domain.Enums.Permission' while attempting to activate 'Gatherly.Infrastructure.Authentication.HasPermissionAttribute'.)

    • @MilanJovanovicTech
      @MilanJovanovicTech  11 หลายเดือนก่อน +1

      Watch Part 3/Part 4 - you need to add a few more things

  • @CaioVitorOliveira
    @CaioVitorOliveira 2 หลายเดือนก่อน

    I'm trying to test this aprouch and I have to initialize my Roles, Permissions, Users, RolePermissions and RoleUsers. My test database starts empty, how can I use linq to initialize those entities?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 หลายเดือนก่อน

      Add them to the DB at startup

    • @CaioVitorOliveira
      @CaioVitorOliveira 2 หลายเดือนก่อน +1

      @@MilanJovanovicTech I'm sorry, I asked the question wrong. By following your videos I was able to implement the HasPermission method and initialize the permissions and related entities using IEntityTypeConfiguration and builder.HasData.
      The problem is in the API tests using xUnit, I use the CustomWebApplicationFactory class that inherits from WebApplicationFactory to initialize my databases but I'm not able to insert the related entities. Could you explain how to seed the Permissions, Roles and RolePermissions in the CustomWebApplicationFactory?

  • @_NguyenHaoTien
    @_NguyenHaoTien 11 หลายเดือนก่อน

    Oh the video is so great,but i have a stuck when apply [authorize(Policy = "A")] for controller but i wanna authorize from Controller except some Action in Controller
    Ex: [authorize(Policy = "A", ExceptAction = "CreateTask")]
    I cannot write new attribute to override, can you help me the solution(.NET 6 or 8)?

    • @MilanJovanovicTech
      @MilanJovanovicTech  11 หลายเดือนก่อน

      AllowAnonymous?

    • @_NguyenHaoTien
      @_NguyenHaoTien 11 หลายเดือนก่อน

      @@MilanJovanovicTech no, i wanna put authorize on action method and ignore authorize on controller

  • @ShaxbozbekQodiov
    @ShaxbozbekQodiov ปีที่แล้ว

    in which video you had writen Enumeration ?

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว

      This one: th-cam.com/video/v6cYTcEfZ8A/w-d-xo.html

  • @velkumars26
    @velkumars26 ปีที่แล้ว +1

    Hi Milan,
    What is the implementation for GetValue()

    • @MilanJovanovicTech
      @MilanJovanovicTech  ปีที่แล้ว

      Where?

    • @velkumars26
      @velkumars26 ปีที่แล้ว

      @@MilanJovanovicTech , I have figured it out. Thank you 😊

    • @shamiljay7847
      @shamiljay7847 ปีที่แล้ว

      @@velkumars26 Can you tell me what it is ?

    • @webdevelopment6679
      @webdevelopment6679 ปีที่แล้ว

      @@velkumars26 Can you share the code ? Or tell me where you found it ?

  • @bykaos2012
    @bykaos2012 2 ปีที่แล้ว +1

    Hello Milan, I get an error when I want to Subscribe from Patreon. I just live on patreon. Anyone else having problems?

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว

      That is very strange. Is it still not functioning? I didn't get any complaints recently.

    • @Tengri9090
      @Tengri9090 2 ปีที่แล้ว

      @@MilanJovanovicTech Yes that is right. I wanted to subscribe too, I got an error. Turkey does not accept credit cards.

  • @K1llRay64
    @K1llRay64 หลายเดือนก่อน

    Why would you use Permission enum as it is, when you introduced to smart enums? Isn't that creating too much chaos when you use both enum and smartenums in your project? and why you need smartenums If you just used Permission enum without hesitation?

    • @MilanJovanovicTech
      @MilanJovanovicTech  หลายเดือนก่อน

      Smart enums have behavior, but also let you simplify seeding

    • @K1llRay64
      @K1llRay64 หลายเดือนก่อน

      @@MilanJovanovicTech thanks for a reply!
      In that case, in a context of DDD, can I use smart Enumeration as a base class for my ValueObject also, in scenarios when I need static properties in my VO?

  • @abidalisidd
    @abidalisidd 2 ปีที่แล้ว

    Hi, I am your new viewer. I want that video link where you created this whole project structure. Please share.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว

      There isn't one starting from scratch

    • @abidalisidd
      @abidalisidd 2 ปีที่แล้ว

      @@MilanJovanovicTech I really like your project structure. Don't you think there should be a video to create a project structure on which you are currently working in this video so that your viewer can write and practice the concept on the same project structure.

  • @thiagomdemelo145
    @thiagomdemelo145 ปีที่แล้ว +1

    If u are using classes that are not default or importable, u need to show us how they work or let we have the code...

  • @prateektomar4874
    @prateektomar4874 8 หลายเดือนก่อน

    please provide the code link.

  • @adewunmivincent4198
    @adewunmivincent4198 5 หลายเดือนก่อน

    How can I get source code

    • @MilanJovanovicTech
      @MilanJovanovicTech  5 หลายเดือนก่อน

      Start from here: th-cam.com/video/4cFhYUK8wnc/w-d-xo.html

  • @pioners1001
    @pioners1001 2 ปีที่แล้ว

    What is the point of storing all the configuration in the database if it does not change (everything is seeded into DB)? If there would be a UI that would allow admins to change permissions for roles, then I would understand why. It would be better to keep it in memory - static classes with configuration would not take a lot of memory and would be much faster.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว

      How difficult would it be to expose a few endpoints for managing it? Not much with all of this in place.
      Also, you get the benefit of having FK constraints between Users and roles/permissions.

    • @pioners1001
      @pioners1001 2 ปีที่แล้ว +1

      @@MilanJovanovicTech That's true, but if you know that permissions and role-permission mapping will never change (or not change in near future), then implementing it with DB store is overkill.
      I have implemented permissions just like in your example but without DB and the system is in production for 3 years and we never had a need to change the mapping. We only add new features and new permissions. Configuration in code works well in such scenario.

    • @MilanJovanovicTech
      @MilanJovanovicTech  2 ปีที่แล้ว

      @@pioners1001 Except changing permissions in your case require downtime. While permissions in the DB can be added/removed without downtime.