Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public MethodResponse <Guid[]> CreateUsers(User[] users)
        {
            List <Guid>             guids       = new List <Guid>();
            MethodResponse <Guid[]> returnCodes = new MethodResponse <Guid[]>();

            foreach (var user in users)
            {
                try
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        guids.Add(UserPersistence.AddUser(user, null));
                        transactionScope.Complete();
                    }
                }
                catch (Exception exception)
                {
                    EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace);
                    returnCodes.AddError(exception, 0);
                    guids.Add(Guid.Empty);
                }
            }
            returnCodes.Result = guids.ToArray();
            return(returnCodes);
        }
        internal static MethodResponse <Guid[]> Create <RecordType, PersistenceType>(this IEnumerable <RecordType> source)
            where RecordType : BaseRecord
            where PersistenceType : DataModelPersistence <RecordType>, new()
        {
            List <Guid>             guids                = new List <Guid>();
            MethodResponse <Guid[]> returnCodes          = new MethodResponse <Guid[]>();
            PersistenceType         datamodelPersistance = new PersistenceType();
            Int32 bulkIndex = 0;

            foreach (RecordType record in source)
            {
                try
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        guids.Add(datamodelPersistance.Create(record));
                        transactionScope.Complete();
                    }
                }
                catch (Exception exception)
                {
                    EventLog.Error("{0}: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace);
                    returnCodes.AddError(exception, bulkIndex);
                }

                bulkIndex += 1;
            }

            returnCodes.Result = guids.ToArray();
            return(returnCodes);
        }
Beispiel #3
0
        public MethodResponse <UserContextData> FindUserByName(string lookupId)
        {
            MethodResponse <UserContextData> response = new MethodResponse <UserContextData>();

            try
            {
                response = UserContextData.GetUserContext(lookupId);
            }
            catch (Exception ex)
            {
                EventLog.Error(ex);
                response.AddError(ex, 0);
            }


            return(response);
        }
Beispiel #4
0
        /// <summary>
        /// Change current user's password
        /// </summary>
        /// <param name="currentPassword"></param>
        /// <param name="newPassword"></param>
        /// <returns></returns>
        public MethodResponse <ErrorCode> ChangePassword(string currentPassword, string newPassword)
        {
            MethodResponse <ErrorCode> response = new MethodResponse <ErrorCode>();

            try
            {
                UserPersistence.ChangePassword(currentPassword, newPassword);
                response.Result = ErrorCode.Success;
            }
            catch (Exception ex)
            {
                EventLog.Error(ex);
                response.AddError(ex, 0);
                response.Result = ErrorCode.NoJoy;
            }

            return(response);
        }
Beispiel #5
0
        public MethodResponse <ErrorCode> RemoveUserFromGroup(string lookupID, Guid groupId)
        {
            MethodResponse <ErrorCode> response = new MethodResponse <ErrorCode>();

            try
            {
                UserPersistence.RemoveUserFromGroup(lookupID, groupId);
                response.Result = ErrorCode.Success;
            }
            catch (Exception ex)
            {
                EventLog.Error(ex);
                response.AddError(ex, 0);
                response.Result = ErrorCode.NoJoy;
            }

            return(response);
        }
Beispiel #6
0
        /// <summary>
        /// Add Organization
        /// </summary>
        /// <param name="organization"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public MethodResponse <Guid> AddOrganization(string organization, string parent)
        {
            MethodResponse <Guid> response = new MethodResponse <Guid>();

            using (TransactionScope transactionScope = new TransactionScope())
            {
                try
                {
                    response.Result = UserPersistence.AddOrganization(organization, parent);
                    transactionScope.Complete();
                }
                catch (Exception ex)
                {
                    EventLog.Error(ex);
                    response.AddError(ex, 0);
                }
            }
            return(response);
        }
Beispiel #7
0
        public MethodResponse <ErrorCode> ResetPassword(string lookupID, string passPhrase)
        {
            MethodResponse <ErrorCode> response = new MethodResponse <ErrorCode>();

            try
            {
                UserPersistence.ResetPassword(lookupID, passPhrase);
                response.Result = ErrorCode.Success;
            }
            catch (Exception ex)
            {
                EventLog.Error(ex);
                response.AddError(ex, 0);
                response.Result = ErrorCode.NoJoy;
            }


            return(response);
        }
Beispiel #8
0
        public MethodResponse <ErrorCode> MustChangePasswordOnNextLogin(string lookupID)
        {
            MethodResponse <ErrorCode> response = new MethodResponse <ErrorCode>();

            try
            {
                UserPersistence.MustChangePasswordOnNextLogin(lookupID);
                response.Result = ErrorCode.Success;
            }
            catch (Exception ex)
            {
                EventLog.Error(ex);
                response.AddError(ex, 0);
                response.Result = ErrorCode.NoJoy;
            }


            return(response);
        }
Beispiel #9
0
        public MethodResponse <ErrorCode> DisableUserAccount(string lookupID)
        {
            MethodResponse <ErrorCode> response = new MethodResponse <ErrorCode>();

            try
            {
                UserPersistence.DisableUserAccount(lookupID);
                response.Result = ErrorCode.Success;
            }
            catch (Exception ex)
            {
                EventLog.Error(ex);
                response.AddError(ex, 0);
                response.Result = ErrorCode.NoJoy;
            }


            return(response);
        }
Beispiel #10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public MethodResponse <Guid> CreateUser(User user, string password)
        {
            MethodResponse <Guid> response = new MethodResponse <Guid>();

            using (TransactionScope transactionScope = new TransactionScope())
            {
                try
                {
                    response.Result = UserPersistence.AddUser(user, password);
                    transactionScope.Complete();
                }
                catch (Exception ex)
                {
                    EventLog.Error(ex);
                    response.AddError(ex, 0);
                    response.Result = Guid.Empty;
                }
            }

            return(response);
        }
Beispiel #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="lookupID"></param>
        /// <returns></returns>
        public MethodResponse <ErrorCode> DeleteUserAccount(string lookupID)
        {
            MethodResponse <ErrorCode> response = new MethodResponse <ErrorCode>();

            using (TransactionScope transactionScope = new TransactionScope())
            {
                try
                {
                    UserPersistence.DeleteUserAccount(lookupID);
                    response.Result = ErrorCode.Success;
                    transactionScope.Complete();
                }
                catch (Exception ex)
                {
                    EventLog.Error(ex);
                    response.AddError(ex, 0);
                    response.Result = ErrorCode.NoJoy;
                }
            }

            return(response);
        }
Beispiel #12
0
        public MethodResponse <ErrorCode> UpdateUser(User[] users)
        {
            MethodResponse <ErrorCode> returnCodes = new MethodResponse <ErrorCode>();

            foreach (var user in users)
            {
                try
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        UserPersistence.UpdateUser(user);
                        transactionScope.Complete();
                    }
                }
                catch (Exception exception)
                {
                    EventLog.Error("{0}, {1}", exception.Message, exception.StackTrace);
                    returnCodes.AddError(exception, 0);
                }
            }
            returnCodes.Result = (returnCodes.HasErrors()) ? ErrorCode.NoJoy : ErrorCode.Success;
            return(returnCodes);
        }
        internal static MethodResponse <ErrorCode> Update <RecordType, PersistenceType>(this IEnumerable <RecordType> source)
            where RecordType : BaseRecord
            where PersistenceType : DataModelPersistence <RecordType>, new()
        {
            MethodResponse <ErrorCode> returnCodes = new MethodResponse <ErrorCode>()
            {
                Result = ErrorCode.Success
            };
            PersistenceType datamodelPersistance = new PersistenceType();
            Int32           bulkIndex            = 0;

            foreach (RecordType record in source)
            {
                try
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        datamodelPersistance.Update(record);
                        transactionScope.Complete();
                    }
                }
                catch (Exception exception)
                {
                    EventLog.Error("{0}: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace);
                    returnCodes.AddError(exception, bulkIndex);
                }

                bulkIndex += 1;
            }

            if (returnCodes.Errors.Length > 0)
            {
                returnCodes.Result = ErrorCode.NoJoy;
            }

            return(returnCodes);
        }
Beispiel #14
0
        /// <summary>
        /// GetUserContext
        /// </summary>
        /// <returns></returns>
        internal static MethodResponse <UserContextData> GetUserContext(string lookupId)
        {
            UserContextData userContextData           = null;
            MethodResponse <UserContextData> response = new MethodResponse <UserContextData>();

            DataModel            dataModel            = new DataModel();
            DataModelTransaction dataModelTransaction = DataModelTransaction.Current;


            try
            {
                UserRow userRow = DataModel.User.UserKeyIdentityName.Find(lookupId.ToLower());
                if (userRow == null)
                {
                    response.AddError("Record Not Found", ErrorCode.RecordNotFound);
                    return(response);
                }

                userRow.AcquireReaderLock(dataModelTransaction);

                //Find AD entry
                DirectoryEntry adUser = ActiveDiretoryHelper.FindUser(userRow.IdentityName);
                if (adUser == null)
                {
                    response.AddError("Record Not Found", ErrorCode.RecordNotFound);
                    return(response);
                }

                //Get the information from AD and the database record
                using (adUser)
                {
                    //Call refresh to update compiled fields.
                    adUser.RefreshCache();

                    userRow.AcquireReaderLock(dataModelTransaction);
                    userContextData          = new UserContextData();
                    userContextData.UserId   = userRow.UserId;
                    userContextData.LookupId = userRow.IdentityName;
                    try
                    {
                        userContextData.PasswordExpires = GetExpiration(adUser);
                    }
                    catch
                    {
                    }


                    userContextData.AccountDisabled = (bool)adUser.Properties["msDS-USerAccountDisabled"].Value;
                    if (adUser.Properties["description"].Value != null)
                    {
                        userContextData.Description = adUser.Properties["description"].Value.ToString();
                    }

                    if (adUser.Properties["displayName"].Value != null)
                    {
                        userContextData.FullName = adUser.Properties["displayName"].Value.ToString();
                    }

                    if (adUser.Properties["mail"].Value != null)
                    {
                        userContextData.EmailAddress = adUser.Properties["mail"].Value.ToString();
                    }

                    userContextData.ServerVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion.ToString();

                    //This is a compiled field and can be null
                    //string msDS = "msDS-User-Account-Control-Computed";
                    if (adUser.Properties["msDS-UserPasswordExpired"].Value != null)
                    {
                        userContextData.IsPasswordExpired = (bool)adUser.Properties["msDS-UserPasswordExpired"].Value;
                    }
                    else
                    {
                        userContextData.IsPasswordExpired = false;
                    }
                }
            }
            catch (Exception ex)
            {
                if (userContextData != null)
                {
                    userContextData = null;
                }
                response.AddError(ex, 0);
            }

            response.Result = userContextData;
            return(response);
        }
        /// <summary>
        /// Delete a set of records from the data model. Each record is deleted in order. The method returns when all the records are successfully
        /// deleted, or when an error occurs - whichever comes first.
        /// </summary>
        /// <typeparam name="RecordType">The specific type of the record.</typeparam>
        /// <typeparam name="PersistenceType">The type that handles deleting the record type.</typeparam>
        /// <param name="source">The record set to delete.</param>
        /// <returns>The errors that may have occurred attempted to delete the set.</returns>
        internal static MethodResponse <ErrorCode> Delete <RecordType, PersistenceType>(this IEnumerable <RecordType> source)
            where RecordType : BaseRecord
            where PersistenceType : DataModelPersistence <RecordType>, new()
        {
            MethodResponse <ErrorCode> returnCodes = new MethodResponse <ErrorCode>()
            {
                Result = ErrorCode.Success
            };
            PersistenceType datamodelPersistance = new PersistenceType();
            Int32           bulkIndex            = 0;

            foreach (RecordType record in source)
            {
                try
                {
                    Boolean finished = false;

                    for (int deadlockRetry = 0; !finished && deadlockRetry < deadlockRetiesMax; deadlockRetry++)
                    {
                        try
                        {
                            using (TransactionScope transactionScope = new TransactionScope())
                            {
                                // This provides a context for any transactions.
                                DataModelTransaction dataModelTransaction = DataModelTransaction.Current;

                                ErrorCode result = datamodelPersistance.Delete(record);

                                if (result == ErrorCode.Success)
                                {
                                    transactionScope.Complete();
                                }
                                else if (result != ErrorCode.RecordNotFound)
                                {
                                    returnCodes.AddError(new ErrorInfo()
                                    {
                                        ErrorCode = result, BulkIndex = bulkIndex
                                    });
                                }

                                // Break out of the deadlock loop if the transaction completed expectedly (either failure or success).
                                finished = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            if (FluidTrade.Core.Utilities.SqlErrorHelper.IsDeadlockException(ex))
                            {
                                if (deadlockRetry == deadlockRetiesMax - 1)
                                {
                                    throw;
                                }
                                if (EventLog.IsLoggingEnabledFor(EventLog.ErrorLogLevel.Verbose))
                                {
                                    EventLog.Warning("Deadlock exception\r\n{0}: {1}", ex.Message, ex.StackTrace);
                                }
                                System.Threading.Thread.Sleep(2000 * deadlockRetry + 1);
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }
                }
                catch (FaultException <RecordNotFoundFault> exception)
                {
                    EventLog.Information("RecordNotFound ignored by delete operation: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace);
                }
                catch (Exception exception)
                {
                    EventLog.Error("{0}: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace);
                    returnCodes.AddError(exception, bulkIndex);
                    break;
                }

                if (returnCodes.HasErrors())
                {
                    break;
                }

                bulkIndex += 1;
            }

            if (returnCodes.Errors.Length > 0)
            {
                returnCodes.Result = ErrorCode.NoJoy;
            }

            return(returnCodes);
        }