add attributes to user entity in jhipster - spring

I want to add some attributes to the user entity, when I googled about it, I found a similar question :
How to modify existing entity generated with jhipster?
when I followed the steps in this post, I couldn't find the file user.json anywhere as #Roberto montioned
1) Edit the json file representing your entity (add/remove field, the syntax is pretty easy, check in the end of the file if is required any change to the general entity properties like 'fieldsContainOneToMany'...), you'll find it in:
<jhipster_root_folder>/.jhipster/entityName.json
How can I solve this ?

I think the best solution is a compromise between the two solutions offered by #Pedro and #alcuvi (who references to the JHipster Documentation):
First, create an "ExtendedUser" entity with the additional fields (don't forget to use git, you will have to undo this/delete the entity). A one-to-one relationship to "User" is not necessary.
After that, you can copy many parts from "ExtendedUser" to the other parts of the JHipster Application:
Liquibase changelog columns (also add them to users.csv)
ExtendedUser.java → User.java and UserDTO.java
extendedUser-dialog.* → register.html/.controller.js and settings.html/.controller.js
Adapt AccountResource.java and UserService.java (and UnitTests if you use them). This step is mostly done by using getters and setters copied in the step before. JHipster Documentation (https://jhipster.github.io/tips/022_tip_registering_user_with_additional_information.html) might be helpful here.
Delete the "ExtendedUser" Entity (using git, or manually, see also: How to delete an entity after creating it using jhipster?)
The *advantages* are:
Using JHipster code generation capabilities
No additional entity (which comes with new DB tables and many files)
I hope this information will help other developers in the future!

The User entity is the entity used by JHipster to manage all user management stuff, like email, passwords, etc., so you won't find a User.json file, since that is an automatically generated entity. Those .json files are only created when you run yo jhipster:entity <entityName>.
In order to add/remove fields to the User entity, you'll have to do it manually, that means editing User.java, creating a liquibase changeset and modify all related files in the UI as needed.

The official documentation of jhipster (version 4) have an entry about this.
https://jhipster.github.io/tips/022_tip_registering_user_with_additional_information.html
In summary...
Its manual solution.
The proposed solution is to create an entity with the fields you want to add to user and linked to it with a one to one relationship.
Alternatively
If you create a ExtendedUser with new fields with JDL-Studio the jhipsterimport-jdl command is going to create "extendedUser" option on entity menu where you can set values for those fields and linked to the user you want.
I thinks is not the best solution...

What about inheritance? Just extend built-in User class with new fields. For example, public class ExtendedUser extends User. And replace User class with ExtendedUser in code. Update dto, service, etc. Also you can use class casting where it's required. What thoughts?

Related

AsyncCrudAppService Breaks Swagger When Providing TCreateInput and TUpdateInput

I recently downloaded a Single Page Web Application (Angular) from https://aspnetboilerplate.com/Templates using 3.x target version.
I just simply added a few entities and then started to follow the steps on this page https://aspnetboilerplate.com/Pages/Documents/Application-Services
Things do work well for me to Get, List, Update, and Delete entities when my app service class is just inheriting AsyncCrudAppService<Entities.PhoneBook, PhoneBookDto, long, GetAllPhoneBooksInput>, however when it is inheriting AsyncCrudAppService<Entities.PhoneBook, PhoneBookDto, long, GetAllPhoneBooksInput, CreatePhoneBookInput, and UpdatePhoneBookInput> the swagger definition will no longer load.
GitHub Repo: https://github.com/woodman231/MyPhoneBooks
(which currently does not work and will not load Swagger page).
I can get the swagger page to load by removing CreatePhoneBookInput and UpdatePhoneBookInput from
https://github.com/woodman231/MyPhoneBooks/blob/main/aspnet-core/src/MyPhoneBooks.Application/SimpleCrudAppServices/ISimplePhoneBookCrudAppService.cs#L9
and
https://github.com/woodman231/MyPhoneBooks/blob/main/aspnet-core/src/MyPhoneBooks.Application/SimpleCrudAppServices/SimplePhoneBookCrudAppService.cs#L14
However, again I am still unable to create entities using this default implementation. Any ideas?
I have cloned your repo and run it and I figured out the error, first as I tell you in comments I verified the text log, and it said the next:
System.InvalidOperationException: Can't use schemaId "$CreatePhoneBookInput" for type "$MyPhoneBooks.SimpleCrudAppServices.Dtos.CreatePhoneBookInput". The same schemaId is already used for type "$MyPhoneBooks.PhoneBooks.Dtos.CreatePhoneBookInput"
What happenig is that you have these two classes UpdatePhoneBookInput, CreatePhoneBookInput repeated in SanokeCrudAppServices\Dtos and PhoneBooks\Dtos
You have the classes in both folders with same exact name, and thats the problem, if you change the name in whatever place the swagger definition will load without errors, I have do it like this and everything works fine!
Change the name in one of the places, and all will be working fine
Personally I don't like to use a different Dto for Create and Update for me is easier to user just one Dto for all.
Ok I figured it out. I had also made a DIY AppService and some of the DTO Class Names associated with the DIY App Service clashed with the DTO Class Names associated with the Automated Service. It was acceptable in .NET since they were in different name spaces but once the swagger definition was configured I assume that there was multiple instances of the same DTO Defition. I checked the AbpLogs table but they didn't give me much details as to the specifics of the internal server error while loading the definition. It sure would have been useful to know that.

Combining metadata from multiple sources

In a SPA app using breeze, how would I go about combining metadata from multiple sources for related data so that I can use them in 1 manager on the client. For example, I might have the following
Entity Framework Metadata from WebAPI controller (e.g. Account)
Custom Metadata from DTOs (e.g. Invoices)
Data from a third party service with metadata provided from client side metadata (e.g. Invoice transmission result)
In each case the data has related properties so I might want to be able to use Account.Transactions.TransmissionResults
UPDATE
I have tried several ways of getting this to work but to no avail. From Jay's answer, it is not possible at present to update the metadata from the server once it has been retrieved, so if and until that changes (see breeze user voice issue) I am left with one of the following approaches
1 Retrieve metadata from the server from Entity Framework and add metadata on the client to add extra entities. This worked to a degree but I could not add navigation properties from entity types added on the client to entity types retrieved from the server because I cannot add the foreign key association to the entity retrieved from the server, again back to the need to modifying metadata after it has been retrieved.
2 Write the complete metadata by hand, which will work but makes maintainability that much harder and seems wrong to be manually writing mostly the same code that the designer would write.
3 Generate most of the code from Entity Framework as described in the docs and then update it afterwards to add in the custom entities. Again similar issues than with option 2, it seems hacky.
Anyone else tried something similar? Is there something I am missing, which I could be, I've only started with breeze and js.
Thanks
A breeze EntityManager can have metadata from any number of DataService endpoints, and you can manually add metadata (new EntityTypes) on the client at any point. The only current restriction is that once you have metadata from a specific service, you can't change it. ( We are considering reviewing the last restriction).
So the question is, what are you trying to do that you can't right now?

Entity Framework 6 DBContext disable delete option

Anyone please help me on how to disable DELETE option in EF6?
I mean from the application, now record should be deleted (even accidentally)
Thanks.
Create a user/role in the database that does not have permissions to delete/modify records and use it in your application. EF itself is not meant to be a security tool and there are always options to perform a delete operation (e.g. a developer can send any arbitrary SQL query/command to the database bypassing all the 'security' measures implemented in the data access layer)
When getting the entities call with AsNoTracking() option.
eg :- Context.Users.AsNoTracking()
Edit after Stevens Comment
Its true that anyone can still go and change the entity state to Deleted manually. I would recommend, using Repository Pattern for data access and can restrict delete operation. By hiding the DbContext outside of the assembly.

Tracking Changes Object Changes

I wanted to get your opinions on the easiest way to track changes that users make when they do CRUD events. I am working on a system where the users are less interested on permissions, but really want to have a sophisticated log of what changes a user made. I am using ASP.NET MVC 3, EF, and NLog.
Any advice is greatly appreciated :)
Steve
I use a convention based approach. Each entity has an associated audit entity which includes all properties from the base entity plus information on the change, including whether it was successful or not. I override the SaveChanges method on the DB context. For each entity being changed it creates an audit entity from it holding the new values. It attempts to save the changes, then uses a separate, auditing context to save each of the audited entities with the results of the save operation. I use an injected utility in the data context to get access to the current user (via HttpContext.Current for web, via the Environment.User for non-web) when constructing the audit entities.
I blogged about an earlier version of this for LINQ to SQL at http://farm-fresh-code.blogspot.com/2009/05/auditing-inserts-and-updates-using-linq.html. You should be able to get the basic idea from that.

using doctrine with codeigniter

I am planning to use doctrine to write a module of my app which is built with codeigniter.
I have a very basic question :
lets say I have a table called "user", with doctrine generate-models from db, 3 classes are generated BaseUser.php, User.php and UserTable.php. Now as I saw in the examples they use User class straigtaway. Should I be doing this ? I need additional business functionality for the user objects. So should I create a codeigniter model user_model and then use User class inside it (aggregation) or somehow extend user class ( i dont know how this will be done as user_model extends model)
Am little confused on this one and cannot locate any appropriate literature for the same.
Any help would be appreciated.
thanks in advance,
For anyone who is interested - I’ve posted up a project starter on my blog - a dev ready incorporation of the following technologies:
ExtJS : client side JS library,
CodeIgniter : presentation + domain tier,
Doctrine : ORM data layer framework
Some features of this project starter are:
- CodeIgniter Models have been replaced with Doctrine Records
- Doctrine is loaded into CI as a plugin
- RoR type before and after filters….
- Doctrine transactions automatically wrapped around every action at execution time (ATOMIC db updates)
Basic Role based security (I think Redux may be in there as well?)
Simply extract, hook up the database.php config file and viola…. You can start coding your layouts, views and models. Probably a few things to iron out - but enjoy!
Hope it helps
GET IT AT: http://thecodeabode.blogspot.com
Check out this info on Doctrine_Table class.
To your 3 generated files:
BaseXXX.php:
Holds the definition of your models so that Doctrine is able to handle the operations on the database. This class tells the ORM what colums are available, their types, advaned functions (like Timestampable,...) and more. You should not put your own data into this file since it will be over-written when re-creating the models from the database.
XXX.php:
Your actual model. This won't be re-created with each new generation process and this is were you keep most of your code. You can overwrite functions of the BaseXXX.php if you have to.
XXXTable.php:
Check my link from the top, This gives you access to the table itself. Personally, I do not use it that often since I put most of the code into XXX.php.
Of course you can create new classes and use them inside your XXX.php file. In order to actually do something with the data (save, read,...) you need classes that are connected (exteneded) from Doctrine's classes.
edit: also check this on a more infos with extending from the Doctrine_Table class

Resources