示例#1
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);
        }
示例#2
0
        public ServerNotificationAddressData CreateNotificationAddress(string localPart, string password, int domainId)
        {
            if (!CoreContext.Configuration.Standalone)
            {
                throw new SecurityException("Only for standalone");
            }

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

            if (string.IsNullOrEmpty(localPart))
            {
                throw new ArgumentNullException("localPart", @"Invalid address username.");
            }

            localPart = localPart.ToLowerInvariant().Trim();

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

            if (!Parser.IsEmailLocalPartValid(localPart))
            {
                throw new ArgumentException(@"Incorrect address username.", "localPart");
            }

            var trimPwd = Parser.GetValidPassword(password);

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

            var notificationAddressSettings = ServerNotificationAddressSettings.LoadForTenant(Tenant);

            if (!string.IsNullOrEmpty(notificationAddressSettings.NotificationAddress))
            {
                RemoveNotificationAddress(notificationAddressSettings.NotificationAddress);
            }

            var utcNow = DateTime.UtcNow;

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

                if (localPart.Length + serverDomain.Name.Length > 318) // 318 because of @ sign
                {
                    throw new ArgumentException(@"Address of mailbox exceed limitation of 319 characters.", "localPart");
                }

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

                var serverDao = daoFactory.CreateServerDao();
                var server    = serverDao.Get(Tenant);

                if (server == null)
                {
                    throw new ArgumentException("Server not configured");
                }

                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      = localPart,
                    Password  = trimPwd,
                    Login     = login,
                    LocalPart = localPart,
                    Domain    = serverDomain.Name,
                    Active    = true,
                    Quota     = 0,
                    Maldir    = maildir,
                    Modified  = utcNow,
                    Created   = utcNow,
                };

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

                engine.SaveMailbox(serverMailbox, serverAddress, false);

                notificationAddressSettings = new ServerNotificationAddressSettings {
                    NotificationAddress = login
                };

                notificationAddressSettings.SaveForTenant(Tenant);

                var mailboxServerDao = daoFactory.CreateMailboxServerDao();

                var smtpSettings = mailboxServerDao.GetServer(server.SmtpSettingsId);

                var address = new MailAddress(login);

                var notifyAddress = new ServerNotificationAddressData
                {
                    Email                  = address.ToString(),
                    SmtpPort               = smtpSettings.Port,
                    SmtpServer             = smtpSettings.Hostname,
                    SmtpAccount            = address.ToLogin(smtpSettings.Username),
                    SmptEncryptionType     = smtpSettings.SocketType,
                    SmtpAuth               = true,
                    SmtpAuthenticationType = smtpSettings.Authentication
                };

                return(notifyAddress);
            }
        }