@@ -11,6 +11,16 @@ import io.github.gitbucket.markedj.Marked
1111import io .github .gitbucket .markedj .Options
1212import org .slf4j .LoggerFactory
1313
14+ import gitbucket .core .model .{GroupMember , Account }
15+ import gitbucket .core .model .Profile ._
16+ import gitbucket .core .util .{StringUtil , LDAPUtil }
17+ import gitbucket .core .service .SystemSettingsService .SystemSettings
18+ import profile .simple ._
19+ import StringUtil ._
20+ import org .slf4j .LoggerFactory
21+ // TODO Why is direct import required?
22+ import gitbucket .core .model .Profile .dateColumnType
23+
1424class AnnounceController extends AnnounceControllerBase
1525with AdminAuthenticator
1626
@@ -19,11 +29,30 @@ trait AnnounceControllerBase extends ControllerBase with AccountService {
1929
2030 private val logger = LoggerFactory .getLogger(classOf [AnnounceController ])
2131
22- case class AnnounceForm (content : String , subject : String )
32+ object EmailAddress {
33+ def isValid (email : String ): Boolean = EmailRegex .pattern.matcher(email.toUpperCase).matches()
34+
35+ private val EmailRegex = """ \b[a-zA-Z0-9.!#$%&¡¯*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\b""" .r
36+ }
37+
38+ def getAccountByGroupName (groupName : String , includeRemoved : Boolean = false )(implicit s : Session ): List [Account ] = {
39+ val needs = GroupMembers
40+ .filter(_.groupName === groupName.bind)
41+ .sortBy(_.userName)
42+ .map(_.userName)
43+ .list
44+
45+ Accounts
46+ .filter(t => (t.userName inSetBind needs) && (t.removed === false .bind, ! includeRemoved))
47+ .list
48+ }
49+
50+ case class AnnounceForm (content : String , subject : String , to : String )
2351
2452 private val announceForm = mapping(
2553 " content" -> trim(label(" Announce" , text(required))),
26- " subject" -> trim(label(" Subject" , text(required)))
54+ " subject" -> trim(label(" Subject" , text(required))),
55+ " to" -> trim(label(" To" , text(required)))
2756 )(AnnounceForm .apply)
2857
2958 get(" /admin/announce" )(adminOnly {
@@ -63,10 +92,19 @@ trait AnnounceControllerBase extends ControllerBase with AccountService {
6392
6493 email.setHtmlMsg(Marked .marked(form.content, opts))
6594
66- logger.info(" sending email: {}" , form.content)
95+ logger.debug(" sending email subject: {}" , form.subject)
96+ logger.debug(" sending email content: {}" , form.content)
6797 val database = Database ()
6898 database withSession { implicit session =>
69- getAllUsers(false ).filter(account => ! account.isGroupAccount && account.mailAddress.nonEmpty).foreach(account => email.addBcc(account.mailAddress))
99+ val mailto = form.to.split(" ," ).map(_.trim).map(groupaccount => {
100+ val userMailAddress : List [Account ] = if (groupaccount.toUpperCase == " ALL" ) getAllUsers(false ) else getAccountByGroupName(groupaccount)
101+ userMailAddress.filter(account => ! account.isGroupAccount && account.mailAddress.nonEmpty && EmailAddress .isValid(account.mailAddress))
102+ }
103+ ).flatMap(a => a).map(_.mailAddress).toSet// .foreach(account => email.addBcc(account.mailAddress)
104+ logger.debug(" sending email to: {}" , form.to)
105+ logger.debug(" sending email to EmailAddress: {}" , mailto.mkString(" ," ))
106+ // getAllUsers(false).filter(account => !account.isGroupAccount && account.mailAddress.nonEmpty && EmailAddress.isValid(account.mailAddress)).foreach(account => email.addBcc(account.mailAddress))
107+ mailto.foreach(mailAddress => email.addBcc(mailAddress))
70108 }
71109
72110 email.send()
0 commit comments