Fabio Carucci's WebLogs

"...You may say I'm a dreamer, but I'm not the only one..." (Imagine)
posts - 51, comments - 41, trackbacks - 27

My Links

News



PER CONSULENZE SCRIVERE SU "CONTACT"

Contattami se sono online!

This is my personal blog. These postings are provided "AS IS" with no warranties, and confer no rights. The views expressed on this weblog are mine alone and do not necessarily reflect the views of my employer.








View Fabio Carucci's profile on LinkedIn

Vuoi ricevere i miei post nella tua email? Iscriviti

Add to Technorati Favorites



http://www.csthota.com/blogmap/



Locations of visitors to this page





Tag Cloud

Archives

Post Categories

1 - Pubblicazioni

2 - Conferenze

3 - Links

4 - Blogs

L'arte di lavorare con le WebPart - #1

"ASP.NET 2.0 fornisce classi che permettono di costruire applicazioni web integrando la tecnologia delle web parts..." si ok questo già lo conosco.

Come spesso accade, però, non sempre basta quello che mamma Microsoft rilascia per le nostre innumerevoli esigenze, così capita che ci si ritrova a dover "estendere" qualcosina.

Iniziamo a estendere, o meglio "rivedere", il Web Part Framework di ASP.NET 2.0 con un bel WebPartManager di casa nostra che si adopera per autorizzare o meno le webparts che gli si pareranno dinanzi nella pagina web. Nel codice che segue da notare AuthorizationFilter, è una proprietà che assume il nostro Custom User Control (o semplicemente uno User Control, di cosa possiamo trasformare in Web Part se ne parlerà in un prossimo post) ereditando da System.Web.UI.WebControls.WebParts.WebPart e permette di specificare, passando una stringa (es. "Administrators, Users, PowerUsers,..."), quali ruoli hanno l'autorità di visualizzare la Web Part in una pagina.

Facile, quindi, verificare il ruoli abilitati in associazione con l'utente corrente

string[] roles = e.AuthorizationFilter.Split(new char[] { ',' });
if (roles.Length > 0)
{
    
if (Roles.Enabled)
        isAuthorized = 
this.RolesManagerIsUserInRoles(roles);
    
else
        
isAuthorized = this.ContextIsUserInRoles(roles);
}               

ed ecco il listato completo

public class WebPartManager : System.Web.UI.WebControls.WebParts.WebPartManager
    {        
        
public WebPartManager() { }        

        
protected override void OnAuthorizeWebPart(WebPartAuthorizationEventArgs e)
        {
            
bool isAuthorized = false;

            
if (string.IsNullOrEmpty(e.AuthorizationFilter) || e.AuthorizationFilter == "*")
            {
                isAuthorized = 
true;
            }
            
else
            
{
                
string[] roles = e.AuthorizationFilter.Split(new char[] { ',' });
                
if (roles.Length > 0)
                {
                    
if (Roles.Enabled)
                        isAuthorized = 
this.RolesManagerIsUserInRoles(roles);
                    
else
                        
isAuthorized = this.ContextIsUserInRoles(roles);
                }                
            }
            e.IsAuthorized = isAuthorized;            
        }
        
        
private bool RolesManagerIsUserInRoles(string[] roles)
        {
            
for (int i = 0; i < roles.Length; i++)
            {
                
if (Roles.IsUserInRole(roles[i]))
                    
return true;
            }
            
return false;
        }
        
        
private bool ContextIsUserInRoles(string[] roles)
        {
            
for (int i = 0; i < roles.Length; i++)
            {
                
if( (Context != null) && (Context.User != null))
                    
if (Context.User.IsInRole(roles[i]))
                        
return true;
            }
            
return false;
        }
    }

 

al prossimo post, in cui rivediamo qualcos'altro.

 

powered by IMHO 1.3

Print | posted on sabato 8 luglio 2006 23.41 | Filed Under [ ASP.NET 2.0 WebPart ]

Feedback

Gravatar

# re: L'arte di lavorare con le WebPart - #1

Finalmente sei tornato a scrivere!!!! Sono molto felice!!
04/10/2006 11.37 | Laura

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 7 and type the answer here:

Powered by: