예제 #1
0
 /// <summary>
 /// Gets an entity given its unique CodeRef value.
 /// </summary>
 ///
 /// <param name="userContext">
 /// User context.
 /// </param>
 ///
 /// <param name="codeRef">
 /// Unique CodeRef value.
 /// </param>
 ///
 /// <returns>
 /// The entity.
 /// </returns>
 public VahapYigit.Test.Models.Role GetByCodeRef(VahapYigit.Test.Core.IUserContext userContext, string codeRef)
 {
     using (var et = new ExecutionTracerService())
         using (var db = new RoleCrud(userContext))
         {
             return(db.GetByCodeRef(codeRef));
         }
 }
예제 #2
0
        public bool Create(IUserContext userContext, ref User user, out IList <TranslationEnum> errors)
        {
            bool withError = false;

            errors = new List <TranslationEnum>();

            using (var userCrud = new UserCrud(userContext))
                using (var roleCrud = new RoleCrud(userContext))
                {
                    // NOTE: if you have a compilation error on 'Role.CodeRefs.Member'
                    //       1. Change Role.CodeRefs.Member by ""
                    //       2. Add the [CodeRef = Member, Name = Member] entry in the 'Role' table
                    //       3. Compile the solution and execute LayerCake Generator
                    //       4. Then change back "" to Role.CodeRefs.Member

                    var memberRole = roleCrud.GetByCodeRef(Role.CodeRefs.Member);

                    if (user.UserRoleCollection.Count(l => l.IdRole == memberRole.Id) == 0)
                    {
                        user.UserRoleCollection.Add(new UserRole {
                            IdUser = user.Id, IdRole = memberRole.Id
                        });
                    }

                    user.RegistrationDate = DateTime.Now;

                    try
                    {
                        userCrud.Save(ref user, new SaveOptions {
                            SaveChildren = true
                        });

                        Task.WaitAll(SendMailOnUserCreatedAsync(user));                 // cannot use await because of ref/out parameters
                    }
                    catch (EntityValidationException evx)
                    {
                        withError = true;
                        errors    = evx.Translations;
                    }
                    catch (System.Net.Mail.SmtpFailedRecipientException)             // Cannot deliver the mail (bad email address?)
                    {
                        // NOTE: if you have a compilation error on 'TranslationEnum.CustomExceptionSmtpBadRecipient'
                        //       1. Comment the line
                        //       2. Add the 'CustomExceptionSmtpBadRecipient' entry in the 'Translation' table
                        //       3. Compile the solution
                        //       4. Execute LayerCake Generator Process (Menu > Extensions > Generate Translations)
                        //       5. Close LayerCake Generator
                        //       6. Then uncomment the line and recompile

                        withError = true;
                        errors.Add(TranslationEnum.CustomExceptionSmtpBadRecipient);
                    }
                    catch
                    {
                        withError = true;
                        throw;
                    }
                    finally
                    {
                        if (withError || errors.Count != 0)
                        {
                            if (user.IsInDb)
                            {
                                userCrud.Delete(user);                         // When the mail has not been delivered... (bad email address?)
                            }
                        }
                    }
                }

            return(!withError && errors.Count == 0);
        }