From: <mar...@us...> - 2010-02-13 21:24:59
|
Revision: 30 http://cronoscontrol.svn.sourceforge.net/cronoscontrol/?rev=30&view=rev Author: marioarce Date: 2010-02-13 21:24:52 +0000 (Sat, 13 Feb 2010) Log Message: ----------- Ticket #5 renamed concept from 'Tracker' to 'Project User'. Project User includes a 'project manager', a 'project team members', and any other project stakeholder The following are examples of project stakeholders: * Project manager * Project team members * Upper management * Project customer * Resource Managers * Product user group * Project testers * Sponsor Added Paths: ----------- source/trunk/CronosControl/Libraries/BusinessLogic/ProjectUser/ source/trunk/CronosControl/Libraries/BusinessLogic/ProjectUser/ProjectUserManager.cs source/trunk/CronosControl/Libraries/BusinessLogic/ProjectUser/ProjectUserSession.cs Added: source/trunk/CronosControl/Libraries/BusinessLogic/ProjectUser/ProjectUserManager.cs =================================================================== --- source/trunk/CronosControl/Libraries/BusinessLogic/ProjectUser/ProjectUserManager.cs (rev 0) +++ source/trunk/CronosControl/Libraries/BusinessLogic/ProjectUser/ProjectUserManager.cs 2010-02-13 21:24:52 UTC (rev 30) @@ -0,0 +1,339 @@ +//------------------------------------------------------------------------------ +// The contents of this file are subject to the GNU General Public License Version 3.0 ("License"); you may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.cronoscontrol.net/license.html. +// +// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. +// See the License for the specific language governing rights and limitations under the License. +// +// The Original Code is CronosControl. +// The Initial Developer of the Original Code is WebImageConsulting http://www.wicnow.com/. +// All Rights Reserved. +// +// Contributor(s): Mario Alberto Arce, _______. +//------------------------------------------------------------------------------ + +using System; +using System.Linq; +using System.Text; +using System.Web.Security; +using System.Collections.Generic; +using System.Configuration.Provider; +using System.Security.Cryptography; +using CronosControl.Model; +using CronosControl.Business; +using CronosControl.Common.Utils; +using CronosControl.BusinessLogic.Configuration.Settings; + +namespace CronosControl.BusinessLogic.ProjectUser +{ + /// <summary> + /// Project User manager + /// </summary> + public partial class ProjectUserManager + { + #region Methods + /// <summary> + /// Adds a project user + /// </summary> + /// <param name="Email">The email</param> + /// <param name="Username">The username</param> + /// <param name="Password">The password</param> + /// <param name="Name">A value indicating whether the name of the project user</param> + /// <param name="Lastname">A value indicating whether the lastname of the project user</param> + /// <param name="Enabled">A value indicating whether the project user is active</param> + /// <param name="status">Status</param> + /// <returns>A project user</returns> + public static User AddProjectUser(string email, string username, string password, + string name, string lastname, + bool enabled, out MembershipCreateStatus status) + { + string saltKey = string.Empty; + string passwordHash = string.Empty; + status = MembershipCreateStatus.UserRejected; + + // duplicated UserName ? + //status = MembershipCreateStatus.DuplicateUserName; + + // invalid UserName ? + //status = MembershipCreateStatus.InvalidUserName; + + // duplicated email ? + //status = MembershipCreateStatus.DuplicateEmail; + + // invalid email ? + if (!CommonHelper.IsValidEmail(email)) + status = MembershipCreateStatus.InvalidEmail; + + + int idCompany = CronosControlContext.Current.CurrentCompany.IdCompany; + passwordHash = CreatePasswordMd5Hash(password); + + User userEntity = new User(); + userEntity.Name = name; + userEntity.Lastname = lastname; + userEntity.Username = username; + userEntity.Password = passwordHash; + userEntity.Email = email; + userEntity.IdCompany = idCompany; + userEntity.CreatedAt = DateTimeHelper.ConvertToUtcTime(DateTime.Now); + userEntity.Enabled = enabled; + + try + { + Users users = new Users(); + users.Save(userEntity); + } + catch + { + status = MembershipCreateStatus.ProviderError; + } + + status = MembershipCreateStatus.Success; + return userEntity; + } + + /// <summary> + /// Adds the specified user names to the specified roles for the configured applicationName + /// </summary> + /// <param name="usernames">A string array of user names to be added to the specified roles.</param> + /// <param name="roleNames">A string array of the role names to add the specified user names to.</param> + public static void AddUsersToRoles(string[] usernames, string[] roleNames) + { + foreach (string username in usernames) + { + // get User + User user = GetProjectUserByUsername(username); + if (user == null) + { + // user not found!, continue... + continue; + } + + foreach (var roleName in roleNames) + { + CronosControlEntities cronosControlEntities = new CronosControlEntities(); + // get Role + Role role = (from r in cronosControlEntities.Role + where r.Name.Equals(roleName) + select r).First<Role>(); + + Project project = CronosControlContext.Current.CurrentProject; + + // assign role to the user + UserProjectRole userProjectRole = new UserProjectRole(); + userProjectRole.Role = role; + userProjectRole.Project = project; + userProjectRole.IdUser = user.IdUser; + + cronosControlEntities.AddToUserProjectRole(userProjectRole); + cronosControlEntities.SaveChanges(); + } + } + } + + /// <summary> + /// Creates a salt + /// </summary> + /// <param name="size">A salt size</param> + /// <returns>A salt</returns> + private static string CreateSalt(int size) + { + RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider(); + byte[] data = new byte[size]; + provider.GetBytes(data); + return Convert.ToBase64String(data); + } + + /// <summary> + /// Creates a password hash + /// </summary> + /// <param name="Password">Password</param> + /// <param name="Salt">Salt</param> + /// <returns>Password hash</returns> + private static string CreatePasswordHash(string Password, string Salt) + { + //MD5, SHA1 + string passwordFormat = SettingManager.Current["Security", "PasswordFormat"]; + if (String.IsNullOrEmpty(passwordFormat)) + passwordFormat = "SHA1"; + + return FormsAuthentication.HashPasswordForStoringInConfigFile(Password + Salt, passwordFormat); + } + + /// <summary> + /// Creates a password Md5 hash + /// </summary> + /// <param name="Password">Password</param> + /// <returns>Password hash</returns> + private static string CreatePasswordMd5Hash(string password) + { + if (string.IsNullOrEmpty(password)) + return string.Empty; + + // Create a new instance of the MD5CryptoServiceProvider object. + MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider(); + + // Convert the input string to a byte array and compute the hash. + byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(password)); + + // Create a new Stringbuilder to collect the bytes + // and create a string. + StringBuilder sBuilder = new StringBuilder(); + + // Loop through each byte of the hashed data + // and format each one as a hexadecimal string. + for (int i = 0; i < data.Length; i++) + { + sBuilder.Append(data[i].ToString("x2")); + } + + // Return the hexadecimal string. + return sBuilder.ToString(); + } + + /// <summary> + /// Gets a list of the roles that a specified user is in for the configured applicationName. + /// </summary> + /// <param name="username">The user to return a list of roles for.</param> + /// <returns>A string array containing the names of all the roles that the specified user is in for the configured applicationName.</returns> + public static string[] GetRolesForUser(string username) + { + // get User + User user = GetProjectUserByUsername(username); + if (user == null) + { + // user not found! + return new string[] { }; + } + + CronosControlEntities cronosControlEntities = new CronosControlEntities(); + + Project project = CronosControlContext.Current.CurrentProject; + + // get UserProjectRole + List<string> userProjectRoles = (from upr in cronosControlEntities.UserProjectRole + join ro in cronosControlEntities.Role + on new { upr.Role.IdRole } equals new { ro.IdRole } + where (upr.IdUser == user.IdUser && upr.Project.IdProject == project.IdProject) + select ro.Name).ToList<string>(); + + string[] result = new string[userProjectRoles.Count]; + for (int i = 0; i < userProjectRoles.Count; i++) + { + result[i] = userProjectRoles[i]; + } + return result; + } + + /// <summary> + /// Gets a tracket by email + /// </summary> + /// <param name="Username">Project user email</param> + /// <returns>A project user</returns> + public static User GetProjectUserByEmail(string email) + { + if (string.IsNullOrEmpty(email)) + return null; + if (!CommonHelper.IsValidEmail(email)) + return null; + + CronosControlEntities cronosControlEntities = new CronosControlEntities(); + User user = (from u in cronosControlEntities.User + where u.Email.Equals(email) + select u).First<User>(); + + return user; + } + + /// <summary> + /// Gets a tracket + /// </summary> + /// <param name="Username">Project user identifier</param> + /// <returns>A project user</returns> + public static User GetProjectUserByID(int projectUserID) + { + if (projectUserID == 0) + return null; + + CronosControlEntities cronosControlEntities = new CronosControlEntities(); + User user = (from u in cronosControlEntities.User + where u.IdUser.Equals(projectUserID) + select u).First<User>(); + + return user; + } + + /// <summary> + /// Gets a tracket by username + /// </summary> + /// <param name="Username">Project user username</param> + /// <returns>A project user</returns> + public static User GetProjectUserByUsername(string username) + { + if (string.IsNullOrEmpty(username)) + return null; + + User user; + CronosControlEntities cronosControlEntities = new CronosControlEntities(); + try + { + user = (from u in cronosControlEntities.User + where u.Username.Equals(username) + select u).First<User>(); + } + catch + { + return null; + } + + return user; + } + + /// <summary> + /// Gets a value indicating whether the specified user is in the specified role for the configured applicationName. + /// </summary> + /// <param name="username">The user name to search for.</param> + /// <param name="roleName">The role to search in.</param> + /// <returns>true if the specified user is in the specified role for the configured applicationName; otherwise, false.</returns> + public static bool IsUserInRole(string username, string roleName) + { + if (string.IsNullOrEmpty(username)) + return false; + if (string.IsNullOrEmpty(roleName)) + return false; + + User user = GetProjectUserByUsername(username); + + CronosControlEntities cronosControlEntities = new CronosControlEntities(); + int result = (from upr in cronosControlEntities.UserProjectRole + where (upr.IdUser == 1 && upr.Role.Name.Equals(roleName)) + select upr).Count<UserProjectRole>(); + + return (result == 1); + } + + /// <summary> + /// Login a project user + /// </summary> + /// <param name="Email">A project user username</param> + /// <param name="Password">Password</param> + /// <returns>Result</returns> + public static bool Login(string username, string password) + { + User user = GetProjectUserByUsername(username); + + if (user == null) + return false; + if (!user.Enabled) + return false; + + string passwordHash = CreatePasswordMd5Hash(password); + bool result = user.Password.Equals(passwordHash); + + // any session stuff here ... + + return result; + } + #endregion + } +} Added: source/trunk/CronosControl/Libraries/BusinessLogic/ProjectUser/ProjectUserSession.cs =================================================================== --- source/trunk/CronosControl/Libraries/BusinessLogic/ProjectUser/ProjectUserSession.cs (rev 0) +++ source/trunk/CronosControl/Libraries/BusinessLogic/ProjectUser/ProjectUserSession.cs 2010-02-13 21:24:52 UTC (rev 30) @@ -0,0 +1,75 @@ +//------------------------------------------------------------------------------ +// The contents of this file are subject to the GNU General Public License Version 3.0 ("License"); you may not use this file except in compliance with the License. +// You may obtain a copy of the License at http://www.cronoscontrol.net/license.html. +// +// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. +// See the License for the specific language governing rights and limitations under the License. +// +// The Original Code is CronosControl. +// The Initial Developer of the Original Code is WebImageConsulting http://www.wicnow.com/. +// All Rights Reserved. +// +// Contributor(s): Mario Alberto Arce, _______. +//------------------------------------------------------------------------------ + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Text; +using System.Web; +using System.Xml; +using CronosControl.Model; + +namespace CronosControl.BusinessLogic.ProjectUser +{ + /// <summary> + /// Represents a project user session + /// </summary> + public partial class ProjectUserSession : BaseEntity + { + #region Constructor + /// <summary> + /// Creates a new instance of the ProjectUserSession class + /// </summary> + public ProjectUserSession() + { + } + #endregion + + #region Properties + /// <summary> + /// Gets or sets the project user session identifier + /// </summary> + public Guid ProjectUserSessionGUID { get; set; } + + /// <summary> + /// Gets or sets the project user identifier + /// </summary> + public int ProjectUserID { get; set; } + + /// <summary> + /// Gets or sets the last accessed date and time + /// </summary> + public DateTime LastAccessed { get; set; } + + /// <summary> + /// Gets or sets a value indicating whether the project user session is expired + /// </summary> + public bool IsExpired { get; set; } + #endregion + + #region Custom Properties + /// <summary> + /// Gets or sets the project user + /// </summary> + public User User + { + get + { + return ProjectUserManager.GetProjectUserByID(ProjectUserID); + } + } + #endregion + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |