Permissions for anything else than BlogsPermission

Posted by Kaibear on 21-Nov-2019 11:30

Hey,

the code example of the BlogsPermission might work. However, there is no other "[Content]Permission" object for coding in Sitefinity.

I wanted to do permissions for Libraries, Images, Albums etc. But there are no LibrariesPermission objects or similar. So how do I apply permissions via code to such items?

Also: How do I configure Globa Permissions?

Is there any detailed code documentation for Sitefinity?

All Replies

Posted by jread on 21-Nov-2019 13:22

Here is the documentation for Permissions API: [View:https://www.progress.com/documentation/sitefinity-cms/for-developers-permissions-api:550:50]

You should be able to follow the pattern here to create modify permissions on Libraries, Images or more with this article: [View:https://www.progress.com/documentation/sitefinity-cms/for-developers-grant-and-deny-permissions:550:50]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Modules.Blogs;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Security.Model;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.Security.Permissions.GrantingDenyingPermissions
{
    public partial class PermissionsApiSnippets
    {
        public static void GrantPermissionsForUser(string userName)
        {
            UserManager usersManager = UserManager.GetManager();
            BlogsManager blogsManager = BlogsManager.GetManager();

            Permission permission = blogsManager.GetPermission(
                BlogsPermissions.Sets.Blog.SetName,
                blogsManager.GetSecurityRoot(false).Id,
                usersManager.GetUser(userName).Id
            );

            permission.GrantActions(
                false,
                BlogsPermissions.Sets.Blog.View,
                BlogsPermissions.Sets.Blog.Create,
                BlogsPermissions.Sets.Blog.Delete
            );

            blogsManager.SaveChanges();
        }
    }
}

Posted by Kaibear on 21-Nov-2019 13:35

Hi @jread ,

yes I was referring to this document.

However, applying this to Libraries for example does not work, as there is no LibrariesPermissions object existing, for example.

Same goes for any other module except Blogs.

That's why I see the documentation a bit unsatisfying for development. It does not seem to cover all cases and does not give information on where it does not work.

In addition, I do not know which strings to use for the PermissionSet, if I need to set in the string itself, for like global permissions. Are the strings equal to the list of Permissionssets: https://www.progress.com/documentation/sitefinity-cms/reference-permission-sets#forms ?


I feel quite lost here.

Posted by jread on 03-Dec-2019 16:46

The Permission set names can be retrieved from Telerik.Sitefinity.Security.SecurityConstants.Set.{NAME}.SetName (for whichever you are applying).  See below SecurityConstants.Album.View

private void CreateAlbumNativeAPI(Guid albumId, string albumTitle)
		{
			UserManager userManager = UserManager.GetManager();
			LibrariesManager librariesManager = LibrariesManager.GetManager();
			Album album = librariesManager.GetAlbums().Where(a => a.Id == albumId).FirstOrDefault();
			
			if (album == null)
			{
				//Create the album.
				album = librariesManager.CreateAlbum(albumId);

				//Set the properties of the album.
				album.Title = albumTitle;
				album.DateCreated = DateTime.UtcNow;
				album.LastModified = DateTime.UtcNow;
				album.UrlName = Regex.Replace(albumTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");

				librariesManager.CreatePermission(SecurityConstants.Sets.Album.View,
					librariesManager.GetSecurityRoot(false).Id,
					userManager.GetUsers().FirstOrDefault(u => u.UserName == "xyz@test.com").Id);

				//Recompiles and validates the url of the album.
				librariesManager.RecompileAndValidateUrls(album);

				//Save the changes.
				librariesManager.SaveChanges();
			}
		}

This thread is closed