More in this section

Forums / General Discussions / Getting mad with Custom Role provider

Getting mad with Custom Role provider

15 posts, 0 answered
  1. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    31 Mar 2011
    Link to this post
    Hello,
    I'm getting stucked with Custom Role Provider and SF 4.0 SP1, I wrote my own custom provider inheriting from RoleDataProvider

    Here it's :
    public class IFRoleProvider : RoleDataProvider
     {
         public IFRoleProvider()
         {
             int t = 0;
         }
     
         public override Telerik.Sitefinity.Security.Model.Role CreateRole(Guid id, string roleName)
         {
             throw new NotImplementedException();
         }
     
         public override Telerik.Sitefinity.Security.Model.Role CreateRole(string roleName)
         {
             throw new NotImplementedException();
         }
     
         public override Telerik.Sitefinity.Security.Model.UserLink CreateUserLink(Guid id)
         {
             Telerik.Sitefinity.Security.Model.UserLink link = CreateUserLink();
     
             link.UserId = id;
             return link;
         }
     
         public override Telerik.Sitefinity.Security.Model.UserLink CreateUserLink()
         {
             Telerik.Sitefinity.Security.Model.UserLink link = new Telerik.Sitefinity.Security.Model.UserLink();
     
             link.ApplicationName = base.ApplicationName;
             return link;
         }
     
         public override void Delete(Telerik.Sitefinity.Security.Model.UserLink item)
         {
             throw new NotImplementedException();
         }
     
         public override void Delete(Telerik.Sitefinity.Security.Model.Role item)
         {
             throw new NotImplementedException();
         }
     
         public override Telerik.Sitefinity.Security.Model.Role GetRole(Guid id)
         {
             using (IDEAEntities entities = new IDEAEntities())
             {
                 Telerik.Sitefinity.Security.Model.Role role = new Telerik.Sitefinity.Security.Model.Role();
     
                 var profilo = entities.SF_PROFILI.Where(a1 => a1.SFID == id).FirstOrDefault();
     
                 if (profilo != null)
                 {
                     role.ApplicationName = base.ApplicationName;
                     role.Id = profilo.SFID;
                     role.Name = profilo.DESCR;
                     role.LastModified = DateTime.MinValue;
                 }
     
                 return role;
             }
         }
     
         public override IQueryable<Telerik.Sitefinity.Security.Model.Role> GetRoles()
         {
             List<Telerik.Sitefinity.Security.Model.Role> lst = new List<Telerik.Sitefinity.Security.Model.Role>();
     
             using (IDEAEntities entities = new IDEAEntities())
             {
                 var profili = entities.SF_PROFILI;
     
                 foreach (var p in profili)
                 {
                     Telerik.Sitefinity.Security.Model.Role role = new Telerik.Sitefinity.Security.Model.Role();
     
                     role = GetRole(p.SFID);
     
                     lst.Add(role);
                 }
             }
     
             return lst.AsQueryable();
         }
     
         public override Telerik.Sitefinity.Security.Model.UserLink GetUserLink(Guid id)
         {
             Telerik.Sitefinity.Security.Model.UserLink userLink = new Telerik.Sitefinity.Security.Model.UserLink();
     
             userLink.Id = id;
             userLink.UserId = id;
             userLink.ApplicationName = base.ApplicationName;
     
            // userLink.Role = GetRoleFromUserLink(id);
     
             var mi = new ManagerInfo();
     
             mi.ApplicationName = base.ApplicationName;
             mi.ManagerType = typeof(ManagerInfo).FullName;
             mi.ProviderName = "IFRoleProvider";
     
             userLink.MembershipManagerInfo = mi;
     
             return userLink;
         }
     
         public override IQueryable<Telerik.Sitefinity.Security.Model.UserLink> GetUserLinks()
         {
             List<Telerik.Sitefinity.Security.Model.UserLink> lst = new List<Telerik.Sitefinity.Security.Model.UserLink>();
     
             using (IDEAEntities entities = new IDEAEntities())
             {
                 var linkProfili = entities.I_UTENTI.Where(o1 => o1.STATO == 0).Select(o1 => o1.SF_PROFILI_UTENTI);
     
                 foreach (var i in linkProfili)
                 {
                     Telerik.Sitefinity.Security.Model.UserLink userLink = GetUserLink(i.GUID);
     
                     lst.Add(userLink);
                 }
             }
     
             return lst.AsQueryable();
         }
     
         private Telerik.Sitefinity.Security.Model.Role GetRoleFromUserLink(Guid userLinkID)
         {
             using (IDEAEntities entities = new IDEAEntities())
             {
                 var userlink = entities.SF_PROFILI_UTENTI.Where(o => o.GUID == userLinkID).FirstOrDefault();
     
                 if (userlink == null) return null;
     
                 Telerik.Sitefinity.Security.Model.Role role = new Telerik.Sitefinity.Security.Model.Role();
     
                 role.ApplicationName = base.ApplicationName;
                 role.Id = userlink.PROFILO.Value;
                 role.Name = userlink.DESCRIZIONE;
                 return role;
             }
         }
     }

    Here's my security.config :

    <?xml version="1.0" encoding="utf-8"?>
    <securityConfig validationKey="5CC9AE80638205978FABD83B1594ADDBBE10F494666BD54C6B2562A2EC83477B14BE55D3A86FD2FB5078D96D55566BC25F88C9E3ED0AF4218E576E9ECF73604A" decryptionKey="E9D557979A99F027A875320031C5E985A8B45495090DBC96FC41076C7BECE57C" xmlns:config="urn:telerik:sitefinity:configuration" xmlns:type="urn:telerik:sitefinity:configuration:type" config:version="4.0.1210.0">
        <applicationRoles>
            <role id="f0c80268-fd71-4cc7-adf0-38f183be05c8" name="Everyone" />
            <role id="2c613c5b-8b24-47ec-af4d-ebb91ab66bd6" name="Anonymous" />
            <role id="c7a2d0f0-574a-4c36-aa16-5c906c074e23" name="Authenticated" />
            <role id="d68b5da4-8ce4-4574-9a1f-783b1ea50414" name="Owner" />
            <role id="93284758-03fe-4d60-847a-377068336680" name="Administrators" />
            <role id="42a991d8-e516-49ef-a1a5-f08ee6b4d9a4" name="BackendUsers" />
            <role id="25a08547-c57a-4717-b473-bbb0e62e156d" name="Authors" />
            <role id="93b56be7-e718-48d3-bde2-94c223fc1236" name="Editors" />
            <role id="4324a0d0-1191-473c-b564-dea9947e6af1" name="Designers" />
        </applicationRoles>
        <securityProviders>
            <add version="4.0.1210.0" name="OpenAccessDataProvider" />
            <add version="4.0.1210.0" name="XmlDataProvider" />
        </securityProviders>
        <membershipProviders>
            <add description="TestIF" resourceClassId="" type="SitefinityWebApp.IFProvider" type:type="System.RuntimeType, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" enabled="True" name="IFProvider" />
        </membershipProviders>
    </securityConfig>

    When I go to the backoffice after having registered it, in the Administration Roles I don't see anything under Default (where I think I should see the default ones) and under mine I see nothing (even if the  public override IQueryable<Telerik.Sitefinity.Security.Model.Role> GetRoles() method is called)

    Any suggestion?
    Thanks
  2. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    31 Mar 2011
    Link to this post
    also in the user management the first time it loads I got all the users (the one present in sitefinity) and the one I've retrived with my IFMembershipProvider (that hinerits from MembershipDataProvider), when I move to the IFMemebershipProvider I got only the one that belongs to that provider, going back to default I see only the ones that belongs to Default (correct)

    is this a bug?
    Thanks
  3. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    31 Mar 2011
    Link to this post
    Hello Telerik,
    today I'm your nightmare ... :D , I'm trying to create a Role (since I cannot display what I've in my collection I tried to create a new one)

    I've implemented the method :

    public override Telerik.Sitefinity.Security.Model.Role CreateRole(Guid id, string roleName)
           {
               Telerik.Sitefinity.Security.Model.Role role = new Telerik.Sitefinity.Security.Model.Role();
     
               role.Id = id;
               role.Name = roleName;
               role.LastModified = DateTime.Now;
                
               role.ApplicationName = base.ApplicationName;
               return role;
           }

    and I get this error:
    ERROR: Item could not be saved.

    How it knows it cannot be saved? where it  tries to save to? Thanks
  4. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    01 Apr 2011
    Link to this post
    Hello Paolo,

    Where do you get this error message? Do you call RoleManager.SaveChanges() after returning the custom Role? We will be releasing custom provider wrappers where we inherit from RoleDataProvider and this is the code that creates a Role:

    public override Role CreateRole(Guid id, string roleName)
    {
        if (id == Guid.Empty)
            throw new ArgumentNullException("id");
     
        if (!String.IsNullOrEmpty(roleName))
        {
            if (this.RoleExists(roleName))
                throw new ProviderException(Res.Get<ErrorMessages>().RoleAlreadyExists.Arrange(roleName));
        }
     
        var newRole = new RoleWrapper(id, roleName);
     
        return newRole;
    }

    RoleWrapper is a simple inheritor of Telerik.Sitefinity.Security.Model.Role to support transactions, you can basically create just Role instread.

    Greetings,
    Liubomir
    the Telerik team
  5. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    01 Apr 2011
    Link to this post
    Hello Liubomir,
    I get this error in the backed after having created the item... no I don't call the RoleManager.SaveChanges() ........ why I don't see the roles in the page?
    Thanks
  6. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    01 Apr 2011
    Link to this post
    Hello,
    I've noticed for what concern the showing of my roles that if I do :

    public override IQueryable<Telerik.Sitefinity.Security.Model.Role> GetRoles()
           {
               List<Telerik.Sitefinity.Security.Model.Role> lst = new List<Role>();
               using (xxxEntities entities = new xxxEntities())
               {
                   var profili = entities.SF_PROFILI;
                   foreach (var p in profili)
                   {
                       Telerik.Sitefinity.Security.Model.Role role = new Telerik.Sitefinity.Security.Model.Role();
                       role.Name = p.DESCR;
                       role.Id = new Guid();
                       //role = GetRole(p.SFID);
                       lst.Add(role);
                   }
               }
               return lst.AsQueryable();
           }

    It works (using new Guid() ), when I set the value of p (that's a GUID taken from DB, using EF, mapped as uniqueidentifier in SQL) it won't work.....
    any suggestion?
    Thanks
  7. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    05 Apr 2011
    Link to this post
    Hi Paolo,

    I'm not very familiar with EF, is p.SFID an actual GUID version 4? There are several kinds of GUIDs - http://en.wikipedia.org/wiki/Universally_unique_identifier - and throughout the entire application we're using GUIDs that are in the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx. Also, if you assign a valid GUID but there already exists a role with this ID an error is most likely to occur. You could try the

    RoleManager.GetRole(roleId)

    to see if there is already a role with this specific id. I think this method will throw an ItemNotFoundException if there isn't a role with this id.

    All the best,
    Liubomir
    the Telerik team

  8. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    05 Apr 2011
    Link to this post
    Hello Liubomir,
    I've understood what I was doing, I had a guid taken from the default provider (when doing some test) and so I've the same ID for an item of Default provider (SF) and mine.... can you please tell me, am I doing right inherits from RoleDataProvider or have I to inherit from RoleProvider? should I also implement some interfaces? I've got bad behaviour using my provider in the administration role section... I load the users (and SF tells me I've got the 753 users I got) but when I see them in the detail I don't see the role I've added...

  9. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    08 Apr 2011
    Link to this post
    Hello Paolo,

    You need to inherit from RoleDataProvider. There are some particularities - you need to implement the following method as well:

    public override ManagerInfo GetManagerInfo(string managerType, string providerName);

    where you set ApplicationName, ManagerType, ProviderName and some generated random Id. This structure is needed in order for your provider to function properly. We will be releasing our RoleProviderWrapper class in the next week. I strongly suggest that you wait till it's released and then you could use Reflector or some other tool to see its source. I noticed that Ivan also provided some suggestion in the other thread so you can check them as well.

    Kind regards,
    Liubomir
    the Telerik team

  10. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    26 Apr 2011
    Link to this post
    Hello Telerik,
    can you please provide me a sample with  MembershipProviderWrapper and RoleProviderWrapper??
    Thanks
  11. Lubomir Velkov
    Lubomir Velkov avatar
    688 posts
    Registered:
    03 Nov 2014
    27 Apr 2011
    Link to this post
    Hi Paolo,

    Attached please find the implementation of MembershipProviderWrapper.

    Greetings,
    Lubomir Velkov
    the Telerik team

  12. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    27 Apr 2011
    Link to this post
    Hello Lubomir,
    thanks for your feedback but I wish to know how to use it, the implementation is good for knowledge but how I inherits from this wrapper or I have to use it?
    Thanks

    Paolo
  13. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    27 Apr 2011
    Link to this post
    Hello Paolo,

    This is the base class with the methods we use. You need to create a class this inherits from MembershipProviderWrapper that comes from Telerik.Sitefinity.Security namespace.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

  14. Paolo
    Paolo avatar
    147 posts
    Registered:
    11 Jun 2009
    27 Apr 2011
    Link to this post
    Hello Ivan,
    so I need do to something as

    public TestProvider(System.Web.Security.MembershipProvider provider ):base(provider)
           {
           }

    where MembershipProvider is a derived class from  System.Web.Security.MembershipProvider provider ?

    then I call the wrapper.Validate?
  15. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    04 May 2011
    Link to this post
    Hi Paolo,

    The way you use is generally used in the constructor when you want make a reference to the class from which you are inheriting

    public class SitefinitySample: SomeBaseClass
        {
         
            public SitefinitySample(string myvalue)
                : base(myvalue)
            {
            }

         
            public override SomeBaseMethod()
            {
              //
            }
        }

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
15 posts, 0 answered