Пример #1
0
        public static ServerMailboxData ToMailboxData(Mailbox mailbox, ServerDomainAddressData address,
                                                      List <ServerDomainAddressData> aliases)
        {
            var userDisplayName = "";

            try
            {
                var user = CoreContext.UserManager.GetUsers(new Guid(mailbox.User));
                userDisplayName = user.DisplayUserName();
            }
            catch
            {
                // skip any errors
            }

            return(new ServerMailboxData
            {
                Id = mailbox.Id,
                UserId = mailbox.User,
                UserDisplayName = userDisplayName,
                Address = address,
                Name = mailbox.Name,
                Aliases = aliases
            });
        }
Пример #2
0
 public static ServerMailboxData ToMailboxData(Mailbox mailbox, ServerDomainAddressData address,
                                               List <ServerDomainAddressData> aliases)
 {
     return(new ServerMailboxData
     {
         Id = mailbox.Id,
         UserId = mailbox.User,
         Address = address,
         Name = mailbox.Name,
         Aliases = aliases
     });
 }
Пример #3
0
        public ServerMailboxData CreateMailbox(string name, string localPart, int domainId, string userId)
        {
            ServerMailboxData mailboxData;

            using (var daoFactory = new DaoFactory())
            {
                var serverDomainDao = daoFactory.CreateServerDomainDao(Tenant);

                var serverDomain = serverDomainDao.GetDomain(domainId);

                var isSharedDomain = serverDomain.Tenant == Defines.SHARED_TENANT_ID;

                if (!IsAdmin && !isSharedDomain)
                {
                    throw new SecurityException("Need admin privileges.");
                }

                var tenantQuota = CoreContext.TenantManager.GetTenantQuota(Tenant);

                if (isSharedDomain &&
                    (tenantQuota.Trial ||
                     tenantQuota.Free))
                {
                    throw new SecurityException("Not available in unpaid version");
                }

                if (string.IsNullOrEmpty(localPart))
                {
                    throw new ArgumentException(@"Invalid local part.", "localPart");
                }

                if (domainId < 0)
                {
                    throw new ArgumentException(@"Invalid domain id.", "domainId");
                }

                if (name.Length > 255)
                {
                    throw new ArgumentException(@"Sender name exceed limitation of 64 characters.", "name");
                }

                Guid user;

                if (!Guid.TryParse(userId, out user))
                {
                    throw new ArgumentException(@"Invalid user id.", "userId");
                }

                if (isSharedDomain && !IsAdmin && user != SecurityContext.CurrentAccount.ID)
                {
                    throw new SecurityException(
                              "Creation of a shared mailbox is allowed only for the current account if user is not admin.");
                }

                var teamlabAccount = CoreContext.Authentication.GetAccountByID(user);

                if (teamlabAccount == null)
                {
                    throw new InvalidDataException("Unknown user.");
                }

                var userInfo = CoreContext.UserManager.GetUsers(user);

                if (userInfo.IsVisitor())
                {
                    throw new InvalidDataException("User is visitor.");
                }

                if (localPart.Length > 64)
                {
                    throw new ArgumentException(@"Local part of mailbox exceed limitation of 64 characters.",
                                                "localPart");
                }

                if (!Parser.IsEmailLocalPartValid(localPart))
                {
                    throw new ArgumentException("Incorrect local part of mailbox.");
                }

                var serverAddressDao = daoFactory.CreateServerAddressDao(Tenant);

                if (serverAddressDao.IsAddressAlreadyRegistered(localPart, serverDomain.Name))
                {
                    throw new DuplicateNameException("You want to create a mailbox with already existing address.");
                }

                if (Defines.ServerDomainMailboxPerUserLimit > 0)
                {
                    var engineFactory = new EngineFactory(Tenant, userId);

                    var accounts = engineFactory.AccountEngine.GetAccountInfoList();

                    var countDomainMailboxes =
                        accounts.Count(a =>
                                       a.IsTeamlabMailbox &&
                                       Parser.ParseAddress(a.Email)
                                       .Domain.Equals(serverDomain.Name, StringComparison.InvariantCultureIgnoreCase));

                    if (countDomainMailboxes >= Defines.ServerDomainMailboxPerUserLimit)
                    {
                        throw new ArgumentOutOfRangeException(
                                  string.Format("Count of user's mailboxes must be less or equal {0}.",
                                                Defines.ServerDomainMailboxPerUserLimit));
                    }
                }

                var serverDao = daoFactory.CreateServerDao();

                var server = serverDao.Get(Tenant);

                var mailboxLocalPart = localPart.ToLowerInvariant();

                var login = string.Format("{0}@{1}", mailboxLocalPart, serverDomain.Name);

                var password = PasswordGenerator.GenerateNewPassword(12);

                var utcNow = DateTime.UtcNow;

                var mailboxDao = daoFactory.CreateMailboxDao();

                using (var tx = daoFactory.DbManager.BeginTransaction(IsolationLevel.ReadUncommitted))
                {
                    var mailbox = new Mailbox
                    {
                        Id               = 0,
                        Tenant           = Tenant,
                        User             = userId,
                        Name             = name,
                        Address          = login,
                        OAuthToken       = null,
                        OAuthType        = (int)AuthorizationServiceType.None,
                        ServerId         = server.ImapSettingsId,
                        Password         = password,
                        SmtpServerId     = server.SmtpSettingsId,
                        SmtpPassword     = password,
                        SizeLast         = 0,
                        MsgCountLast     = 0,
                        BeginDate        = Defines.MinBeginDate,
                        Imap             = true,
                        Enabled          = true,
                        IsTeamlabMailbox = true,
                        IsRemoved        = false,
                        DateCreated      = utcNow
                    };

                    mailbox.Id = mailboxDao.SaveMailBox(mailbox);

                    var address = new ServerAddress
                    {
                        Id          = 0,
                        Tenant      = Tenant,
                        MailboxId   = mailbox.Id,
                        DomainId    = serverDomain.Id,
                        AddressName = localPart,
                        IsAlias     = false,
                        IsMailGroup = false,
                        DateCreated = utcNow
                    };

                    address.Id = serverAddressDao.Save(address);

                    var engine = new Server.Core.ServerEngine(server.Id, server.ConnectionString);

                    var maildir = PostfixMaildirUtil.GenerateMaildirPath(serverDomain.Name, localPart, utcNow);

                    var serverMailbox = new Server.Core.Entities.Mailbox
                    {
                        Name      = name,
                        Password  = password,
                        Login     = login,
                        LocalPart = localPart,
                        Domain    = serverDomain.Name,
                        Active    = true,
                        Quota     = 0,
                        Maldir    = maildir,
                        Modified  = utcNow,
                        Created   = utcNow,
                    };

                    var serverAddress = new Alias
                    {
                        Name     = name,
                        Address  = login,
                        GoTo     = login,
                        Domain   = serverDomain.Name,
                        IsActive = true,
                        IsGroup  = false,
                        Modified = utcNow,
                        Created  = utcNow
                    };

                    engine.SaveMailbox(serverMailbox, serverAddress);

                    tx.Commit();

                    CacheEngine.Clear(userId);

                    mailboxData = ToMailboxData(mailbox, ToServerDomainAddressData(address, login),
                                                new List <ServerDomainAddressData>());
                }
            }

            return(mailboxData);
        }