Example #1
0
        /// <summary>
        /// Updates the user.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="user">The user.</param>
        /// <param name="orchardUsers">The orchard users.</param>
        /// <returns></returns>
        private bool UpdateUser(ContentItem item, User user, List<ContentItem> orchardUsers = null)
        {
            bool isUpdated = false;
            try
            {
                var title = item.As<Orchard.Core.Title.Models.TitlePart>();
                if (title.Title != user.FullName)
                {
                    isUpdated |= true;
                    title.Title = user.FullName;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Nastala chyba pri update title!", ex);
            }

            if (item.As<LocalizationPart>().Culture == null)
            {
                try
                {
                    SetCulture(item, Constants.DEFAULTADLANGUAGE, null);
                }
                catch (Exception ex)
                {
                    throw new Exception("Nastala chyba pri update culture!", ex);
                }
            }

            //XmlSerializer xs = new XmlSerializer(typeof(User));

            //using (var sw = new StringWriter())
            //{
            //    using (var xw = XmlWriter.Create(sw))
            //    {
            //        xs.Serialize(xw, user);
            //    }

            //    throw new Exception(String.Format("User data: {0}", sw.ToString()));
            //}
            ContentPart employee;
            try
            {
                var contentItem = ((dynamic)item);
                employee = (ContentPart)contentItem.Employee;
            }
            catch (Exception ex)
            {
                throw new Exception("Nastala chyba pri vytahovani employee z content item!", ex);
            }
            if (employee != null)
            {
                foreach (var field in employee.Fields)
                {
                    switch (field.Name.ToLower().Trim())
                    {
                        case Constants.DEPARTMENT:
                            try
                            {
                                isUpdated |= CheckValue(field, user.Department);
                            }
                            catch (Exception ex)
                            { throw new Exception("Nastala chyba pri update department!", ex); }
                            break;
                        case Constants.OFFICE:
                            try
                            {
                                isUpdated |= CheckValue(field, user.Office);
                            }
                            catch (Exception ex)
                            { throw new Exception("Nastala chyba pri update office!", ex); }
                            break;
                        case Constants.JOBTITLE:
                            try
                            {
                                isUpdated |= CheckValue(field, user.JobTitle);
                            }
                            catch (Exception ex)
                            { throw new Exception("Nastala chyba pri update jobtitle!", ex); }
                            break;
                        case Constants.PHONE:
                            try
                            {
                                isUpdated |= CheckValue(field, user.PhoneNo);
                            }
                            catch (Exception ex)
                            { throw new Exception("Nastala chyba pri update phone!", ex); }
                            break;
                        case Constants.PRIMARYEMAIL:
                            try
                            {
                                isUpdated |= CheckValue(field, user.Email);
                            }
                            catch (Exception ex)
                            { throw new Exception("Nastala chyba pri update primaryemail!", ex); }
                            break;
                        case Constants.SECONDARYEMAIL:
                            try
                            {
                                isUpdated |= CheckValue(field, user.SecondaryEmail);
                            }
                            catch (Exception ex)
                            { throw new Exception("Nastala chyba pri update secondaryemail!", ex); }
                            break;
                        case Constants.USERID:
                            try
                            {
                                //len koli novym pouzivatelom, vtedy je povodny field null
                                isUpdated |= CheckValue(field, Convert.ToBase64String(user.UserID));
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("Nastala chyba pri update userid!", ex);
                            }
                            break;
                        case Constants.PHOTO:
                            try
                            {
                                if (user.Photo != null)
                                {
                                    var filename = String.Format("{0}.{1}", user.LoginName, Constants.PHOTOEXTENSION);

                                    var imageField = ((MediaLibraryPickerField)field);

                                    var anyMediaPart = !imageField.Ids.Any();

                                    if (anyMediaPart || !CompareFileContent(imageField.MediaParts.FirstOrDefault().FolderPath, imageField.MediaParts.FirstOrDefault().FileName, user.Photo))
                                    {

                                        var mediaFile = mediaLibraryService.ImportMedia(new MemoryStream(user.Photo), "Zamestnanci", filename);
                                        try
                                        {
                                            orchardServices.ContentManager.Create(mediaFile);
                                        }
                                        catch (Exception ex)
                                        {
                                            throw new Exception(String.Format("Nastala chyba pri pridavani fotky pre pouzivatela:{0}", user.LoginName), ex);
                                        }
                                        imageField.Ids = new[] { mediaFile.Id };
                                        isUpdated |= true;
                                        SetPhotos(orchardUsers, mediaFile.Id);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("Nastala chyba pri updatovani fotky!", ex);
                            }
                            break;
                    }
                }
            }

            if (isUpdated)
                try
                {
                    orchardServices.ContentManager.Publish(item);
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Nastala chyba pri publish usera: {0}, content item: {1}", user.LoginName, item.Parts.Count()), ex);
                }
            return isUpdated;
        }
Example #2
0
 /// <summary>
 /// Creates the new user.
 /// </summary>
 /// <param name="ldapService">The LDAP service.</param>
 /// <param name="item">The item.</param>
 /// <returns></returns>
 private bool CreateNewUser(ILdapService ldapService, User item)
 {
     var pass = RandomString(10);
     var user = originalMembershipService.Value.CreateUser(new CreateUserParams(item.LoginName, pass, null, null, null, true));
     if (user != null)
     {
         user.As<UserLdapPart>().LdapDirectoryId = ldapService.Directory.Id;
         return true;
     }
     return false;
 }
Example #3
0
        /// <summary>
        /// Creates the user.
        /// </summary>
        /// <param name="ldapService">The LDAP service.</param>
        /// <param name="user">The user.</param>
        /// <param name="parentContentItem">The parent content item.</param>
        /// <param name="language">The language.</param>
        /// <returns></returns>
        private ContentItem CreateUser(ILdapService ldapService, User user, ContentItem parentContentItem, string language)
        {
            var orchardUser = CreateEmployee(user, parentContentItem, language);
            if (orchardUser == null)
            {
                Logger.Error("An error occured during creating employee: {0}, in DirectoryServices module!", user.Surname);
            }
            if (originalMembershipService.Value.GetUser(user.LoginName) == null)
            {
                if (!CreateNewUser(ldapService, user))
                {
                    Logger.Error("An error occurred during creating user: {0}, in ADModule.", user.Surname);
                }
            }

            return orchardUser;
        }
Example #4
0
        /// <summary>
        /// Creates the employee.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <param name="parentContentItem">The parent content item.</param>
        /// <param name="language">The language.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception">
        /// </exception>
        private ContentItem CreateEmployee(User user, ContentItem parentContentItem, string language)
        {
            ContentItem orchardUser;
            try
            {
                orchardUser = _contentManager.New(Constants.CONTENTTYPENAME);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Nastala chyba pri vytvarani noveho employee content item pre usera: {0}", user.LoginName), ex);
            }
            orchardUser.As<Orchard.Core.Title.Models.TitlePart>().Title = user.FullName;
            try
            {
                orchardServices.ContentManager.Create(orchardUser, VersionOptions.Draft);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Nastala chyba pri volani create employee content item pre usera: {0}", user.LoginName), ex);
            }
            SetCulture(orchardUser, language, parentContentItem);

            try
            {
                UpdateUser(orchardUser, user);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Nastala chyba pri aktualizacii udajov pouzivatela: {0}", user.LoginName), ex);
            }

            return orchardUser;
        }
Example #5
0
        /// <summary>
        /// Checks all user language variantions.
        /// </summary>
        /// <param name="parentContentItem">The parent content item.</param>
        /// <param name="orchardUsers">The orchard users.</param>
        /// <param name="languages">The languages.</param>
        /// <param name="ADuser">a duser.</param>
        /// <exception cref="System.Exception">
        /// </exception>
        private void CheckAllUserLanguageVariantions(ContentItem parentContentItem, List<ContentItem> orchardUsers, List<string> languages, User ADuser)
        {
            if (orchardUsers.Count == languages.Count)
                return;
            foreach (var language in languages)
            {
                if (language == parentContentItem.As<LocalizationPart>().Culture.Culture)
                    continue;
                var user = orchardUsers.FirstOrDefault(u => u.As<LocalizationPart>().Culture.Culture == language);
                if (user != null)
                {
                    try
                    {
                        UpdateUser(parentContentItem, ADuser, orchardUsers);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(string.Format("Nastala chyba pri aktualizacii udajov pouzivatela: {0}, users: {1}", ADuser.LoginName, orchardUsers.Count), ex);
                    }
                    continue;
                }
                try
                {
                    CreateEmployee(ADuser, parentContentItem, language);
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Chyba pri vytvarani zamestnanca, pocet orchard users: {0}", orchardUsers.Count), ex);
                }
            }

        }