public async Task<User> Get(User user)
        {
            User _result = null;

            if (user.Domain != null)
            {
                if (user.Guid != null && user.Guid != Guid.Empty)
                {
                    _result = db.Users.FirstOrDefault(u => u.Guid == user.Guid);
                    return _result;
                }

                if (!string.IsNullOrEmpty(user.LdapSid))
                {
                    _result = db.Users.FirstOrDefault(u => u.LdapSid == user.LdapSid);
                    return _result;
                }

                if (!string.IsNullOrEmpty(user.Login) && user.Domain != null)
                {
                    _result = db.Users.FirstOrDefault(u => u.Login == user.Login && u.Domain.Id == user.Domain.Id);
                    return _result;
                }
            }

            return _result;
        }
 public void UpdateModel(User user, ILdapUser ldapUser)
 {
     user.Guid = ldapUser.Guid;
     user.Name = ldapUser.Name;
     user.LdapSid = ldapUser.Sid;
     user.DisplayName = ldapUser.DisplayName;
     user.Description = ldapUser.Description;
     user.Created = ldapUser.Created;
     user.Updated = ldapUser.Updated;
     user.EmployeeId = ldapUser.EmployeeId;
     user.Email = ldapUser.Email;
 }
 public ExchMailbox CastMailbox(User user, string template)
 {
     return new ExchMailbox()
     {
         Domain = user.Domain.Name,
         Identity = user.Login,
         Type = template,
         OwaEnabled = false,
         Pop3Enabled = false,
         ImapEnabled = false,
         ActiveSyncEnabled = false
     };
 }
 public ExchMailbox CastMailbox(User user)
 {
     return new ExchMailbox()
     {
         Domain = user.Domain.Name,
         Identity = user.Login,
         //Type = user.Type.Name,
         OwaEnabled = false,
         Pop3Enabled = false,
         ImapEnabled = false,
         ActiveSyncEnabled = false
     };
 }
 public LdapUser CastLdapUser(User user)
 {
     return new LdapUser()
     {
         Sid = user.LdapSid,
         Name = user.Name,
         SamAccountName = user.Login,
         UserPrincipalName = user.Login + "@" + user.Domain.Fqdn,
         Domain = CoreManager.Instance.GetDomainConfig(user.Domain),
         GivenName = user.GivenName,
         Surname = user.Surname,
         DisplayName = user.DisplayName,
         Description = user.Description
     };
 }
 public User CastDboUser(User user, LdapUser ldapUser)
 {
     return new User()
     {
         Login = user.Login,
         Guid = ldapUser.Guid != Guid.Empty ? ldapUser.Guid : Guid.NewGuid(),
         LdapSid = ldapUser.Sid,
         //Name = user.Name ?? string.Format("{0} {1} {2} ({3})", user.Surname, user.GivenName, user.MiddleName, user.Login),
         GivenName = user.GivenName,
         Surname = user.Surname,
         //DisplayName = user.DisplayName ?? string.Format("{0} {1} {2}", user.Surname, user.GivenName, user.MiddleName),
         Description = user.Description,
         //Domain = WorkflowManager.User. dbo.Domain.GetByName(user.Domain),
         Info = user.Info
     };
 }
 public NewUserResponse Create(User user, string password)
 {
     return new NewUserResponse()
     {
         Domain = user.Domain.Name,
         Id = user.Id,
         Login = user.Login,
         Password = password,
         Name = user.Name,
         Mail = user.Email,
         DisplayName = user.DisplayName,
         Description = user.Description,
         Info = user.Info,
         LdapSid = user.LdapSid,
         //Type = user.Type.Name
     };
 }
        public async Task<User> Update(User user)
        {
            //db.Users.Attach(user);

            var _entity = db.Users.Find(user.Id);
            db.Entry(_entity).CurrentValues.SetValues(user);
            
            //db.Entry(user).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return user;
        }
        public async Task<User> Create(User user)
        {
            User _user = await Get(user);

            if (_user == null)
            {
                if(user.Guid == Guid.Empty) { user.Guid = Guid.NewGuid(); }

                db.Users.Add(user);
                await db.SaveChangesAsync();

                _user = db.Users.FirstOrDefault(u => u.Guid == user.Guid);
            }
            else
            {
                user.Id = _user.Id;
                //await Update(user);
                throw new Exception("Cannot create user. User exist. [" + log.Name + "]");
            }
            
            return _user;
        }
 public BaseUserWorkflow(User user) : base ()
 {
     this.user = user;
 }
 internal void UpdateModel(User user, User dboUser)
 {
     user.Id = dboUser.Id;
 }
 internal void UpdateModel(User user, ExchMailbox mailbox)
 {
     user.Email = mailbox.Address;
 }
 public CreateUserWorkflow(User user) : base(user) { }