Programmatically Adding Images to Liferay Image Gallery - liferay

I've created a Liferay portlet that uploads image files to the Image Gallery. I am already able to add and retrieve images from my Liferay server programmatically via IGImageLocalServiceUtil, but I can't seem to see the images in the Image Gallery portlet under Control Panel. I suspect this has something to do with permission issues. Could you please help me find out what's wrong in my code? Thanks.
long userId = themeDisplay.getUserId();
long groupId = themeDisplay.getLayout().getGroupId();
String newFilename = "test";
long folderId = 0;
String[] permissions = { "VIEW" };
serviceContext.setCommunityPermissions( permissions );
serviceContext.setGuestPermissions( permissions );
igImage = IGImageLocalServiceUtil.addImage( userId, groupId, folderId, newFilename, "", sourceFile, "image/png" ,serviceContext);
IGImageLocalServiceUtil.addImageResources(igImage, serviceContext.getCommunityPermissions(), serviceContext.getGuestPermissions());

I got it now. What I was not able to mention in my question is that this portlet is located in the Control Panel. So whenever I get the groupId via themeDisplay.getLayout().getGroupId() instead of the site's own groupId, the image is associated with the Control Panel and not the site.
I should use themeDisplay.getDoAsGroupId() instead of themeDisplay.getLayout().getGroupId() then.

Related

How to access Rich Text Field Image outside SalesForce?

I'm using SalesForce object to store user information, it includes profile image too. I'm able to insert images in Rich Text Field by sending Base64 String along with <img> tag. Image gets stored and displayed in the SalesForce page. No problem with that.
Also I'm able to get the image url to the client side (Mobile App) with the help of Rest API (NodeJS).The problem here is, Image URL is accessible(browser) only if the SalesForce account is logged in. If else it goes to the SalesForce login page (When I try with other brower).
What I want is to display the images in my Mobile app which doesn't have SalesForce login access. But I can provide enough information to client side if needed.
My image url looks like this,
https://c.ap5.content.force.com/servlet/rtaImage?eid=a037F00000YSybT&feoid=00N7F00000Pupl6&refid=0EM7F000000lIN0
Also, I have some of the security information in my server side (Node) which I got when I successfully login to SalesForce. BTW I'm using node-salesforce package to establish SalesForce connection.
1) organizationId
2) instanceUrl
3) userID
4) accessToken
I have been stuck with this problem since a week before and I'm running out of deadline. Please HELP!!!
Thanks!
I ended up rewriting the urls in the rich text on the fly when returning them from my api. In a manner so that they refer to my api which I use as image proxy through this SF API endpoint with an integration user: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/resources_sobject_rich_text_image_retrieve.htm
private void rewriteImageUrls(RichTextRecord richTextRecord) {
Document doc = Jsoup.parse(richTextRecord.getBody());
doc.select("img").forEach(imgElement -> {
try {
URL originalUrl = new URL(imgElement.absUrl("src"));
String newUrl = appEnv.getBackendUrl() + "/api/v1/rich_text_records/" + richTextRecord.getSfid() + "/image?refid="+ UrlUtils.getQueryMap(originalUrl.getQuery()).get("refid");
imgElement.attr("src", newUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
});
richTextRecord.setBody(doc.outerHtml());
}
Far from ideal but it works!

Logged in user can only access 1 page?

Using Orchard 1.6 Iv created a new role 'FactoryWorker'. When this user logs in from the front end I want them to be navigated to one page only.
OrchardLocal/System/ManufacturedProducts
I have set this page to be a print screen of the order details so the factory worker will know what products to get ready for ship out & they wont be able to navigate as no menu appears, but also need the other pages blocked incase the user decides to enter the URL of a page they arnt allowed access to.
This is the only page I want this particular user to be able to access(after they login), and I have added a logout button, which logs out the user and returns them to the home page.
So iv been looking through editing a role, with permissions and content etc...but this all seems to be applying to forms and content in general. where the user can access any content type etc...
So can someone advise me on how to do this?
thanks for any replies
UPDATE
I forgot to mention that this is not a content type, item or part I am talking about.
I have created my own controller & View & VM which is accessible from the dash board (using the AdminMenu, which brings the admin user to OrchardLocal/System/ManufacturedProducts)
I have looked at Orchard.ContentPermissions Feature but it only seems to allow me to 1)Grant permissions for others or 2)Grant permission for own content
any ideas?
You can use a Request Filter, (I do not know if it is the best way) :
FilterProvider – defines the filter applied to each request. Resembles the way default ASP.NET MVC action filters work with the difference that it’s not an attribute. All FilterProvider objects are injected into the request pipeline and are applied to all requests (so you need to check if the current request is suitable for your filter at the beginning of an appropriate method).
From : http://www.szmyd.com.pl/blog/most-useful-orchard-extension-points
So you could implement something like this
public class Filter : FilterProvider, IAuthorizationFilter {
private readonly IAuthenticationService _authenticationService;
public Filter(IAuthenticationService authenticationService) {
_authenticationService = authenticationService;
}
public void OnAuthorization(AuthorizationContext filterContext) {
//If route is the restricted one
if (filterContext.HttpContext.Request.Url.AbsoluteUri.Contains("OrchardLocal/System/ManufacturedProducts")) {
//Get the logged user
IUser loggedUser = _authenticationService.GetAuthenticatedUser();
if (loggedUser == null)
return filterContext.Result = new HttpUnauthorizedResult();
//Get the Roles
var roles = loggedUser.As<IUserRoles>().Roles;
if (!roles.Contains("FactoryUser")) {
//User is not authorized
return filterContext.Result = new HttpUnauthorizedResult();
}
}
}
}
Note: Untested code!
EDIT: Also you could invert the logic and check if the logged user has the role 'FactoryUser' and restrict its access to every page except the one they should see.
Your module can create a new permission (look at one of the permissions.cs files for examples), then create a role that has only that permission. Have your controller action check that permission (again, many examples found by finding usage of the permissions defined in one of the permissions.cs).
You can use the Content Permissions module. Using this module you can attach a content item permission part to a content type. This part allows you to choose which roles can see the content when you create it.

Liferay search does not provide results for web-contents

Our company is using Liferay portal 6.0.5 CE.
Within liferay we have a Community, community has few members and few web-contents.
By default all members have role Community member and they can view these web-contents. But if user tries to search web content via liferay search, it does not provide any results.
If I set permissions for web-contents such that Guest role can also view, then search provides the relevant results.
If it matters, before we had a Liferay 5.x and have migrated to 6.0 & then to 6.0.5.
Any ideas for this?
Thanks.
It seems that the guest view permissions for articles have not been set when migrating from 5.0 (if at all permissions have been set before for guest users). Easy fix would be assigning guest view permissions for all the articles that you want to make visible for guest in the respective community. this line in the below code assigns view permissions
JournalArticleLocalServiceUtil.addArticleResources(groupId,
article.getArticleId(), new String[] { "VIEW" },
new String[] { "VIEW" });
its been referred as resource and you will be adding permissions to the Article Resources.
ThemeDisplay themeDisplay = (ThemeDisplay) request
.getAttribute(WebKeys.THEME_DISPLAY);
long groupId = themeDisplay.getLayout().getGroupId();
List<JournalArticle> articles = JournalArticleLocalServiceUtil
.getArticles(groupId);
for (JournalArticle article : articles) {
JournalArticleLocalServiceUtil.addArticleResources(groupId,
article.getArticleId(), new String[] { "VIEW" },
new String[] { "VIEW" });
}
Thank you for advice, but I solved this trouble via hook for search portlet. For searching I used DynamicQuery and PermissionChecker and simple method String.contains(). It works bit slower, but it works--at least for 1k+ web contents!

Getting the GUID for any given path to sharepoint

I'm trying to get the GUID of a given sharepoint URL. I don't mind using the API or the webservices or Sharepoint's database.
if i were to write a function, it's signature would be:
//get a GUID from path.
string GetGuidFromPath(string path){}
I had a lead: SPContentMapProvider but it doesn't seem to get the right info.
Thank you!
Depends - what's the context of the current request? Is your code running in the context of a SharePoint request? If so you can just use SPContext.Current.Web.ID
Otherwise, is your code at least running on one of the SharePoint servers? If so you'll need to use:
// Given the URL http://mysharepointsite.com/sites/somesite/somesubsite
using(SPSite site = new SPSite("http://mysharepointsite.com/sites/somesite"))
{
using(SPWeb web = site.OpenWeb("somesubsite"))
{
Guid webId = web.ID;
}
// Or
Guid rootWebId = site.RootWeb.ID;
}

Reset page library permission for anonymouse user in SharePoint

In the Site Collection level there is a Page library, which has been created along with the whole site. I didn't touch it for long time until recently I added new content types, modified some page layouts and master pages using a solution file. When accessing the home page using the site configured for anonymouse user it failed with "401 UNAUTHORIZED" error on the web page. I noticed the URL was /_layouts/AccessDenied.aspx?Source=...&Type=list&name={...} Then I copy this string to the site for authenticated user, it appears as "Error: Access Denied". I also checked the guid of name={...}. It's the page library list ID.
It seemed the page library permission is not correctly set. However the other page libraries of sub sites are all working well under anonymous user, using the same new content type, master page and page layouts. Their permission are identical on the settings page (all inherit from parent) and all have "allow anonymous" enabled.
I also tried create pages with other page layouts in that page library, clean up content types, all didn't help.
It's not the permission issue of the page library, rather it is related to the code I modified. SPContext.Current.Site.RootWeb is the the refernce by anonymous user. That user do not have privilege to access root folder. I assumed that SPContext.Current.Site.RootWeb.RootFolder.WelcomePage would work with elevated privilege, but after some reading I realized it's not elevate the privilege as I thought. Here's an explanation.
bool rtn = false;
SPWeb rootWeb = SPContext.Current.Site.RootWeb;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(rootWeb.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
rtn = web.Url + "/" + web.RootFolder.WelcomePage
== this.Page.Request.Url.AbsoluteUri;
}
}
});
return rtn;

Resources