ServerUserListServerPlugin.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » WebDashboard » Plugins » ServerReport » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » WebDashboard » Plugins » ServerReport » ServerUserListServerPlugin.cs
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Xml;
using Exortech.NetReflector;
using ThoughtWorks.CruiseControl.Core.Reporting.Dashboard.Navigation;
using ThoughtWorks.CruiseControl.Core.Util;
using ThoughtWorks.CruiseControl.Remote;
using ThoughtWorks.CruiseControl.WebDashboard.Dashboard;
using ThoughtWorks.CruiseControl.WebDashboard.IO;
using ThoughtWorks.CruiseControl.WebDashboard.MVC;
using ThoughtWorks.CruiseControl.WebDashboard.MVC.Cruise;
using ThoughtWorks.CruiseControl.WebDashboard.MVC.View;
using ThoughtWorks.CruiseControl.WebDashboard.Plugins.ProjectReport;
using ThoughtWorks.CruiseControl.WebDashboard.ServerConnection;
using System;
using ThoughtWorks.CruiseControl.Remote.Security;

namespace ThoughtWorks.CruiseControl.WebDashboard.Plugins.ServerReport{
    /// <title>User List Server Plugin</title>
    /// <version>1.5</version>
    /// <summary>
    /// Displays all the users in the system, plus the security rights they have on the server.
    /// </summary>
    /// <example>
    /// <code title="Minimalist Example">
    /// &lt;serverUserListServerPlugin /&gt;
    /// </code>
    /// <code title="Full Example">
    /// &lt;serverUserListServerPlugin resetPassword="anewpassword" /&gt;
    /// </code>
    /// </example>
    /// <remarks>
    /// <para type="tip">
    /// This can be installed using the "User List" package.
    /// </para>
    /// </remarks>
    [ReflectorType("serverUserListServerPlugin")]
    public class ServerUserListServerPlugin : ICruiseAction, IPlugin
    {
        #region Private consts
        private const string ActionName = "ViewServerUserList";
        private const string DiagnosticsActionName = "ViewUserSecurityDiagnostics";
        #endregion

        #region Private fields
        private readonly IFarmService farmService;
        private readonly IVelocityViewGenerator viewGenerator;
        private readonly ISessionRetriever sessionRetriever;
        private readonly IUrlBuilder urlBuilder;
        private string resetPassword = "password";
        #endregion

        #region Constructors
        public ServerUserListServerPlugin(IFarmService farmService, 
            IVelocityViewGenerator viewGenerator, 
            ISessionRetriever sessionRetriever,
            IUrlBuilder urlBuilder)
        {
            this.farmService = farmService;
            this.viewGenerator = viewGenerator;
            this.sessionRetriever = sessionRetriever;
            this.urlBuilder = urlBuilder;
        }
        #endregion

        #region Public properties
        public string LinkDescription
        {
            get { return "View User List"; }
        }

        public INamedAction[] NamedActions
        {
            get
            {
                return new INamedAction[] { new ImmutableNamedAction(ActionName, this),
                        new ImmutableNamedActionWithoutSiteTemplate(DiagnosticsActionName, this)
                    };
            }
        }

        /// <summary>
        /// The password to use when reseting the password.
        /// </summary>
        /// <version>1.5</version>
        /// <default>None</default>
        [ReflectorProperty("resetPassword", Required = false)]
        public string ResetPassword
        {
            get { return resetPassword; }
            set { resetPassword = value; }
        }
        #endregion

        #region Public methods
        public IResponse Execute(ICruiseRequest request)
        {
            string userName = HttpContext.Current.Request.Form["user"];
            string action = HttpContext.Current.Request.Form["action"];
            if (string.IsNullOrEmpty(userName))
            {
                return GenerateUserList(request, string.Empty, string.Empty);
            }
            else
            {
                if (action == "Reset password")
                {
                    string message = string.Empty;
                    string errorMsg = string.Empty;
                    try
                    {
                        farmService.ResetPassword(request.ServerName, sessionRetriever.SessionToken, userName, resetPassword);
                        message = "The password has been reset";
                    }
                    catch (Exception error)
                    {
                        errorMsg = error.Message;
                    }
                    return GenerateUserList(request, message, errorMsg);
                }
                else
                {
                    return GenerateUserDiagnostics(request, userName);
                }
            }
        }
        #endregion

        #region Private methdods
        private IResponse GenerateUserDiagnostics(ICruiseRequest request, string userName)
        {
            Hashtable velocityContext = new Hashtable();

            velocityContext["userName"] = userName;
            velocityContext["message"] = string.Empty;
            string sessionToken = request.RetrieveSessionToken(sessionRetriever);
            if (!string.IsNullOrEmpty(request.ProjectName))
            {
                velocityContext["projectName"] = request.ProjectName;
                velocityContext["diagnostics"] = farmService.DiagnoseSecurityPermissions(request.ProjectSpecifier, sessionToken, userName);
            }
            else
            {
                velocityContext["diagnostics"] = farmService.DiagnoseSecurityPermissions(request.ServerSpecifier, sessionToken, userName);
            }

            return viewGenerator.GenerateView(@"UserDiagnostics.vm", velocityContext);
        }

        private IResponse GenerateUserList(ICruiseRequest request, string message, string error)
        {
            Hashtable velocityContext = new Hashtable();
            velocityContext["message"] = message;
            velocityContext["error"] = error;

            ArrayList links = new ArrayList();
            links.Add(new ServerLink(request.UrlBuilder, request.ServerSpecifier, "User List", ActionName));

            ProjectStatusListAndExceptions projects = farmService.GetProjectStatusListAndCaptureExceptions(request.ServerSpecifier, request.RetrieveSessionToken());
            foreach (ProjectStatusOnServer projectStatusOnServer in projects.StatusAndServerList)
            {
                DefaultProjectSpecifier projectSpecifier = new DefaultProjectSpecifier(projectStatusOnServer.ServerSpecifier, projectStatusOnServer.ProjectStatus.Name);
                links.Add(new ProjectLink(request.UrlBuilder, projectSpecifier, projectSpecifier.ProjectName, ServerUserListServerPlugin.ActionName));
            }
            velocityContext["projectLinks"] = links;
            string sessionToken = request.RetrieveSessionToken(sessionRetriever);
            List<UserDetails> allUsers = farmService.ListAllUsers(request.ServerSpecifier, sessionToken);
            foreach (UserDetails user in allUsers)
            {
                if (user.DisplayName == null) user.DisplayName = string.Empty;
            }
            velocityContext["users"] = allUsers;
            if (!string.IsNullOrEmpty(request.ProjectName))
            {
                velocityContext["currentProject"] = request.ProjectName;
                velocityContext["diagnosticsCall"] = new ProjectLink(request.UrlBuilder, request.ProjectSpecifier, string.Empty, DiagnosticsActionName);
            }
            else
            {
                velocityContext["diagnosticsCall"] = new ServerLink(request.UrlBuilder, request.ServerSpecifier, string.Empty, DiagnosticsActionName);
            }

            return viewGenerator.GenerateView(@"UserList.vm", velocityContext);
        }
        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.