Django admin - User and group extend - python-3.x

Iam trying to update my user model in Django's admin Panel.
I want to add a field/column in Admin panel named "Group" for Users. This field will have the option to select any value from the existing Groups (single option only/Dropdown).
I tried to search for the document but I couldnt found out the relevant information to manipulate the User Admin panel. Although I do found a few blogs and video where they have created a new app and extend the User model.
Is it possible to update Admin panel for User? Please suggest any document or blog or any approach to achieve the goal.

You will probably have to extend the user model in models.py, and also to use Inlines to reflect the changes to the admin site. Both of these steps are performed by Julia in this video: https://www.youtube.com/watch?v=sXZ3ntGp_Xc
The documentation for Inlines can be found here.

So in your admin.py you can extend UserAdmin and in Permissions if you add groups it will create a group assignment to your user create form in admin.
class CustomUserAdmin(UserAdmin):
add_form = UserCreateForm
fieldsets = (
(None, {'fields': ('email')}),
('Permissions', {'fields': (('is_active', 'groups'), )}),
)

Related

Django: Register models to admin site dynamically on request

I have some django models registered to my admin site by declaring them in my admin.py file. I however do not want specific internal users to be able to see certain tables. Is there a way to dynamically registered models to the admin site when a request is is received?
e.g something like if request.user.email has a .gmail domain don't register the user's table.
admin.py
from django.contrib.admin import AdminSite
admin_site = MyAdminSite()
for model_name, model in app.models.items():
model_admin = type(model_name + "Admin", (admin.ModelAdmin, ), {'list_display': tuple([field.name for field in model._meta.fields])})
admin.site.register(model, model_admin)

User entity with one to one relation has empty drop down

I am using Jhipster 4.13.3
In the jdl, I have the following relation.
relationship OneToOne {
UserInfo{user(userName)} to User{userInfo}
}
UserInfo table has extra user detail and relates to other tables I have.
In the UI, I went to Entities -> User Info -> "Create new User info"
In this form, User input is present, but drop down is empty. I was hoping to see admin, user and all new users there.
What is the simple way to link User and UserInfo from the UI?
Thanks.
My mistake. userName doesn't exist in User. I replaced with login and it is working now.

If New Document/Media uploaded in liferay by admin, user has to get message after login

I am very new to liferay. Please help me implementing the below requirement.
Using Document and media portlet in liferay, If any new document is uploaded or uploaded document is modified(Version changed) by admin user, then
How can i identify that the particular document is modified or newly uploaded as i have to show a popup message to user based upon if any new files is uploaded or modified after log on.
That is not a little change request - this required bit more development. And here is more different variants:
Simple but nonperformance variant:
With UserLocalServiceUtil you can check the last user-login date
Similarly iterate over all documents and check last modification date
Create Liferay-Portlet that shows the list of documents with modification date after last user-login date
~
Here are the steps:
Use corresponding Document Listener i.e
DlFolderListener or DlFileEntryListener. You have to use hook to
add your listner in portal.properties.
For Example, you would need to workaround below property.
value.object.listener.com.liferay.portlet.documentlibrary.model.FileEntry
= com.my.custom.MyFileEntryListener
This class would be extending BaseModelListener<FileEntry>
Override and use onAfterUpdate method to notify appropriate audience
(users).
Now this can be done by setting this notification in user
preferences.
On user Login, check corresponding user preferences for this
notification and notify user. You can use hook LoginPostAction to read user preferences for notification.
Hope this helps.
Create customfield for user. Create table with service builder to store the fileEntry Id which modified.
Create DLFileEntry Listener and write
code on FileUpdate. Add DLFileEntryID in same table created in step 1. Set
custom field true for all the user.
Create LoginPostActionHook and on Check the user's flag and fetch the FileEntryId get info of that fileEntryId and display notification with all file's information. Set customfield Flag false for particular user and remove the fileentryid from table or mark them all as read.

Displaying additional profile fields that are synced with AD using JavaScript

Along with the thumbnail photo, I may want to display other properties in my master pages that are imported from AD such as "company" using User Profile sync
If I use SPServices.SPGetCurrentUser() (https://spservices.codeplex.com/documentation), I can get selected properties held in user profile settings. However, I can't make this call since the property does not exist here (yet).
var company = $().SPServices.SPGetCurrentUser({
fieldName: "Company",
debug: false
});
It is however, is displayed both in http://mysite.mydomain.com/_layouts/EditProfile.aspx when in Mysite and _layouts/ProfAdminEdit.aspx (Edit user properties in Central Admin). I guess my question is then to be able to use SPServices, do I somehow edit the default properties and include my "Company" attribute held in the user' mysite profile? Alternatively, is there another way to access the properties held in the user profile with JavaScript ?
Thanks
Daniel
$().SPServices.SPGetCurrentUser calls this page : http://you.site.com/_layouts/userdisp.aspx?Force=True&1376982818371. For me the function is not able to parse the page correctly, but you could simply use jQuery (or pure JS or whatever) to get by yourself the same page, and then parse it to find the data you want.
Otherwise you can use $SP().people() to query the User Profile Service and gets the info for the user. See the example from the provided link. In theory that should return you the same information or even more info.

Accessing the user from a liferay portlet?

I'm attempting to develop a portlet for liferay.
How can I get access to the username and password (and other data liferay has) of the user that's currently logged in?
I'd also like to be able to execute some code when users change their password.
You can get the User ID by calling getRemoteUser() in the PortletRequest object. This is defined by JSR-168 therefore it's cross-portal compatible.
Once you have the ID you can fetch the additional informations by calling getUserById() (a Liferay specific service). This is something not covered by Portlet API specification, so it locks you to the Liferay.
Liferay Specific stuff, here is a code sample to be written in your Portlet Class to retrieve the User:
ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(WebKeys.THEME_DISPLAY);
User user = themeDisplay.getRealUser(); // it gives you the actual Logged in User
//you can also use
// User user = themeDisplay.getUser(); // this would fetch the User you are impersonating
long userId = user.getUserId();
String userName = user.getEmailAddress();
Alternatively;
long userId = themeDisplay.getRealUserId(); // themeDisplay.getUserId();
User user = UserLocalServiceUtil.getUser(userId);
Impersonate User:
Liferay has a concept that admins (or persons with the correct set of permissions) can impersonate a particular user of the portal. Through this they can see how the portal looks to that user.
For executing the code when user change their passwords:
One approach would be to create a hook plugin and overriding the services by extending the UserLocalServiceWrapper class. Then checking for the password change and executing your code inside the your custom class.
Hope this helps.
Or you can just use javascript:
Liferay.ThemeDisplay.getUserId()
There are many nice to haves in the Liferay namespace, take a look at the not so well documented API:
https://www.liferay.com/community/wiki/-/wiki/Main/Liferay+JavaScript+API
https://www.liferay.com/web/pankaj.kathiriya/blog/-/blogs/usage-of-liferay-js-object
Also, take a look at the web services available under localhost:8080/api/jsonws which you can invoke with a javascript call:
Liferay.Service(
'/user/get-user-by-id',
{
userId: 10199
},
function(obj) {
console.log(obj);
}
);
One simple and easy way to get the user in Liferay is PortalUtil.getUser function.
User user = PortalUtil.getUser(portletRequest);

Resources