001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.jsonwebservice.JSONWebService;
019    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceMode;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    import com.liferay.portal.model.Account;
022    import com.liferay.portal.model.Address;
023    import com.liferay.portal.model.Company;
024    import com.liferay.portal.model.EmailAddress;
025    import com.liferay.portal.model.Phone;
026    import com.liferay.portal.model.RoleConstants;
027    import com.liferay.portal.model.Website;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.security.permission.PermissionChecker;
030    import com.liferay.portal.service.base.CompanyServiceBaseImpl;
031    import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
032    
033    import java.io.InputStream;
034    
035    import java.util.List;
036    
037    /**
038     * Provides the local service for accessing, adding, checking, and updating
039     * companies. Its methods include permission checks. Each company refers to a
040     * separate portal instance.
041     *
042     * @author Brian Wing Shun Chan
043     * @author Julio Camarero
044     */
045    @JSONWebService
046    public class CompanyServiceImpl extends CompanyServiceBaseImpl {
047    
048            /**
049             * Adds a company.
050             *
051             * @param  webId the company's web domain
052             * @param  virtualHost the company's virtual host name
053             * @param  mx the company's mail domain
054             * @param  shardName the company's shard
055             * @param  system whether the company is the very first company (i.e., the
056             * @param  maxUsers the max number of company users (optionally
057             *         <code>0</code>)
058             * @param  active whether the company is active
059             * @return the company
060             * @throws PortalException if the web domain, virtual host name, or mail
061             *         domain was invalid or if the user was not a universal
062             *         administrator
063             */
064            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
065            @Override
066            public Company addCompany(
067                            String webId, String virtualHost, String mx, String shardName,
068                            boolean system, int maxUsers, boolean active)
069                    throws PortalException {
070    
071                    PermissionChecker permissionChecker = getPermissionChecker();
072    
073                    if (!permissionChecker.isOmniadmin()) {
074                            throw new PrincipalException();
075                    }
076    
077                    return companyLocalService.addCompany(
078                            webId, virtualHost, mx, shardName, system, maxUsers, active);
079            }
080    
081            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
082            @Override
083            public Company deleteCompany(long companyId) throws PortalException {
084                    PermissionChecker permissionChecker = getPermissionChecker();
085    
086                    if (!permissionChecker.isOmniadmin()) {
087                            throw new PrincipalException();
088                    }
089    
090                    return companyLocalService.deleteCompany(companyId);
091            }
092    
093            /**
094             * Deletes the company's logo.
095             *
096             * @param  companyId the primary key of the company
097             * @throws PortalException if the company with the primary key could not be
098             *         found or if the company's logo could not be found or if the user
099             *         was not an administrator
100             */
101            @Override
102            public void deleteLogo(long companyId) throws PortalException {
103                    if (!roleLocalService.hasUserRole(
104                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
105    
106                            throw new PrincipalException();
107                    }
108    
109                    companyLocalService.deleteLogo(companyId);
110            }
111    
112            /**
113             * Returns the company with the primary key.
114             *
115             * @param  companyId the primary key of the company
116             * @return Returns the company with the primary key
117             * @throws PortalException if a company with the primary key could not be
118             *         found
119             */
120            @Override
121            public Company getCompanyById(long companyId) throws PortalException {
122                    return companyLocalService.getCompanyById(companyId);
123            }
124    
125            /**
126             * Returns the company with the logo.
127             *
128             * @param  logoId the ID of the company's logo
129             * @return Returns the company with the logo
130             * @throws PortalException if the company with the logo could not be found
131             */
132            @Override
133            public Company getCompanyByLogoId(long logoId) throws PortalException {
134                    return companyLocalService.getCompanyByLogoId(logoId);
135            }
136    
137            /**
138             * Returns the company with the mail domian.
139             *
140             * @param  mx the company's mail domain
141             * @return Returns the company with the mail domain
142             * @throws PortalException if the company with the mail domain could not be
143             *         found
144             */
145            @Override
146            public Company getCompanyByMx(String mx) throws PortalException {
147                    return companyLocalService.getCompanyByMx(mx);
148            }
149    
150            /**
151             * Returns the company with the virtual host name.
152             *
153             * @param  virtualHost the company's virtual host name
154             * @return Returns the company with the virtual host name
155             * @throws PortalException if the company with the virtual host name could
156             *         not be found or if the virtual host was not associated with a
157             *         company
158             */
159            @Override
160            public Company getCompanyByVirtualHost(String virtualHost)
161                    throws PortalException {
162    
163                    return companyLocalService.getCompanyByVirtualHost(virtualHost);
164            }
165    
166            /**
167             * Returns the company with the web domain.
168             *
169             * @param  webId the company's web domain
170             * @return Returns the company with the web domain
171             * @throws PortalException if the company with the web domain could not be
172             *         found
173             */
174            @Override
175            public Company getCompanyByWebId(String webId) throws PortalException {
176                    return companyLocalService.getCompanyByWebId(webId);
177            }
178    
179            /**
180             * Removes the values that match the keys of the company's preferences.
181             *
182             * This method is called by {@link
183             * com.liferay.portlet.portalsettings.action.EditLDAPServerAction} remotely
184             * through {@link com.liferay.portal.service.CompanyService}.
185             *
186             * @param  companyId the primary key of the company
187             * @param  keys the company's preferences keys to be remove
188             * @throws PortalException if the user was not an administrator
189             */
190            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
191            @Override
192            public void removePreferences(long companyId, String[] keys)
193                    throws PortalException {
194    
195                    if (!roleLocalService.hasUserRole(
196                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
197    
198                            throw new PrincipalException();
199                    }
200    
201                    companyLocalService.removePreferences(companyId, keys);
202            }
203    
204            /**
205             * Updates the company
206             *
207             * @param  companyId the primary key of the company
208             * @param  virtualHost the company's virtual host name
209             * @param  mx the company's mail domain
210             * @param  maxUsers the max number of company users (optionally
211             *         <code>0</code>)
212             * @param  active whether the company is active
213             * @return the company with the primary key
214             * @throws PortalException if a company with the primary key could not be
215             *         found or if the new information was invalid or if the user was
216             *         not a universal administrator
217             */
218            @Override
219            public Company updateCompany(
220                            long companyId, String virtualHost, String mx, int maxUsers,
221                            boolean active)
222                    throws PortalException {
223    
224                    PermissionChecker permissionChecker = getPermissionChecker();
225    
226                    if (!permissionChecker.isOmniadmin()) {
227                            throw new PrincipalException();
228                    }
229    
230                    return companyLocalService.updateCompany(
231                            companyId, virtualHost, mx, maxUsers, active);
232            }
233    
234            /**
235             * Updates the company with additional account information.
236             *
237             * @param  companyId the primary key of the company
238             * @param  virtualHost the company's virtual host name
239             * @param  mx the company's mail domain
240             * @param  homeURL the company's home URL (optionally <code>null</code>)
241             * @param  logo whether to update the company's logo
242             * @param  logoBytes the new logo image data
243             * @param  name the company's account name (optionally <code>null</code>)
244             * @param  legalName the company's account legal name (optionally
245             *         <code>null</code>)
246             * @param  legalId the company's account legal ID (optionally
247             *         <code>null</code>)
248             * @param  legalType the company's account legal type (optionally
249             *         <code>null</code>)
250             * @param  sicCode the company's account SIC code (optionally
251             *         <code>null</code>)
252             * @param  tickerSymbol the company's account ticker symbol (optionally
253             *         <code>null</code>)
254             * @param  industry the the company's account industry (optionally
255             *         <code>null</code>)
256             * @param  type the company's account type (optionally <code>null</code>)
257             * @param  size the company's account size (optionally <code>null</code>)
258             * @return the the company with the primary key
259             * @throws PortalException if a company with the primary key could not be
260             *         found or if the new information was invalid or if the user was
261             *         not an administrator
262             */
263            @Override
264            public Company updateCompany(
265                            long companyId, String virtualHost, String mx, String homeURL,
266                            boolean logo, byte[] logoBytes, String name, String legalName,
267                            String legalId, String legalType, String sicCode,
268                            String tickerSymbol, String industry, String type, String size)
269                    throws PortalException {
270    
271                    if (!roleLocalService.hasUserRole(
272                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
273    
274                            throw new PrincipalException();
275                    }
276    
277                    return companyLocalService.updateCompany(
278                            companyId, virtualHost, mx, homeURL, logo, logoBytes, name,
279                            legalName, legalId, legalType, sicCode, tickerSymbol, industry,
280                            type, size);
281            }
282    
283            /**
284             * Updates the company with addition information.
285             *
286             * @param  companyId the primary key of the company
287             * @param  virtualHost the company's virtual host name
288             * @param  mx the company's mail domain
289             * @param  homeURL the company's home URL (optionally <code>null</code>)
290             * @param  logo if the company has a custom logo
291             * @param  logoBytes the new logo image data
292             * @param  name the company's account name (optionally <code>null</code>)
293             * @param  legalName the company's account legal name (optionally
294             *         <code>null</code>)
295             * @param  legalId the company's accout legal ID (optionally
296             *         <code>null</code>)
297             * @param  legalType the company's account legal type (optionally
298             *         <code>null</code>)
299             * @param  sicCode the company's account SIC code (optionally
300             *         <code>null</code>)
301             * @param  tickerSymbol the company's account ticker symbol (optionally
302             *         <code>null</code>)
303             * @param  industry the the company's account industry (optionally
304             *         <code>null</code>)
305             * @param  type the company's account type (optionally <code>null</code>)
306             * @param  size the company's account size (optionally <code>null</code>)
307             * @param  languageId the ID of the company's default user's language
308             * @param  timeZoneId the ID of the company's default user's time zone
309             * @param  addresses the company's addresses
310             * @param  emailAddresses the company's email addresses
311             * @param  phones the company's phone numbers
312             * @param  websites the company's websites
313             * @param  properties the company's properties
314             * @return the company with the primary key
315             * @throws PortalException the company with the primary key could not be
316             *         found or if the new information was invalid or if the user was
317             *         not an administrator
318             */
319            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
320            @Override
321            public Company updateCompany(
322                            long companyId, String virtualHost, String mx, String homeURL,
323                            boolean logo, byte[] logoBytes, String name, String legalName,
324                            String legalId, String legalType, String sicCode,
325                            String tickerSymbol, String industry, String type, String size,
326                            String languageId, String timeZoneId, List<Address> addresses,
327                            List<EmailAddress> emailAddresses, List<Phone> phones,
328                            List<Website> websites, UnicodeProperties properties)
329                    throws PortalException {
330    
331                    Company company = updateCompany(
332                            companyId, virtualHost, mx, homeURL, logo, logoBytes, name,
333                            legalName, legalId, legalType, sicCode, tickerSymbol, industry,
334                            type, size);
335    
336                    updateDisplay(company.getCompanyId(), languageId, timeZoneId);
337    
338                    updatePreferences(company.getCompanyId(), properties);
339    
340                    UsersAdminUtil.updateAddresses(
341                            Account.class.getName(), company.getAccountId(), addresses);
342    
343                    UsersAdminUtil.updateEmailAddresses(
344                            Account.class.getName(), company.getAccountId(), emailAddresses);
345    
346                    UsersAdminUtil.updatePhones(
347                            Account.class.getName(), company.getAccountId(), phones);
348    
349                    UsersAdminUtil.updateWebsites(
350                            Account.class.getName(), company.getAccountId(), websites);
351    
352                    return company;
353            }
354    
355            /**
356             * Updates the company with additional account information.
357             *
358             * @param      companyId the primary key of the company
359             * @param      virtualHost the company's virtual host name
360             * @param      mx the company's mail domain
361             * @param      homeURL the company's home URL (optionally <code>null</code>)
362             * @param      name the company's account name (optionally
363             *             <code>null</code>)
364             * @param      legalName the company's account legal name (optionally
365             *             <code>null</code>)
366             * @param      legalId the company's account legal ID (optionally
367             *             <code>null</code>)
368             * @param      legalType the company's account legal type (optionally
369             *             <code>null</code>)
370             * @param      sicCode the company's account SIC code (optionally
371             *             <code>null</code>)
372             * @param      tickerSymbol the company's account ticker symbol (optionally
373             *             <code>null</code>)
374             * @param      industry the the company's account industry (optionally
375             *             <code>null</code>)
376             * @param      type the company's account type (optionally
377             *             <code>null</code>)
378             * @param      size the company's account size (optionally
379             *             <code>null</code>)
380             * @return     the the company with the primary key
381             * @throws     PortalException if a company with the primary key could not
382             *             be found or if the new information was invalid or if the user
383             *             was not an administrator
384             * @deprecated As of 7.0.0, replaced by {@link #updateCompany(long, String,
385             *             String, String, boolean, byte[], String, String, String,
386             *             String, String, String, String, String, String)}
387             */
388            @Deprecated
389            @Override
390            public Company updateCompany(
391                            long companyId, String virtualHost, String mx, String homeURL,
392                            String name, String legalName, String legalId, String legalType,
393                            String sicCode, String tickerSymbol, String industry, String type,
394                            String size)
395                    throws PortalException {
396    
397                    return updateCompany(
398                            companyId, virtualHost, mx, homeURL, true, null, name, legalName,
399                            legalId, legalType, sicCode, tickerSymbol, industry, type, size);
400            }
401    
402            /**
403             * Updates the company with addition information.
404             *
405             * @param      companyId the primary key of the company
406             * @param      virtualHost the company's virtual host name
407             * @param      mx the company's mail domain
408             * @param      homeURL the company's home URL (optionally <code>null</code>)
409             * @param      name the company's account name (optionally
410             *             <code>null</code>)
411             * @param      legalName the company's account legal name (optionally
412             *             <code>null</code>)
413             * @param      legalId the company's accout legal ID (optionally
414             *             <code>null</code>)
415             * @param      legalType the company's account legal type (optionally
416             *             <code>null</code>)
417             * @param      sicCode the company's account SIC code (optionally
418             *             <code>null</code>)
419             * @param      tickerSymbol the company's account ticker symbol (optionally
420             *             <code>null</code>)
421             * @param      industry the the company's account industry (optionally
422             *             <code>null</code>)
423             * @param      type the company's account type (optionally
424             *             <code>null</code>)
425             * @param      size the company's account size (optionally
426             *             <code>null</code>)
427             * @param      languageId the ID of the company's default user's language
428             * @param      timeZoneId the ID of the company's default user's time zone
429             * @param      addresses the company's addresses
430             * @param      emailAddresses the company's email addresses
431             * @param      phones the company's phone numbers
432             * @param      websites the company's websites
433             * @param      properties the company's properties
434             * @return     the company with the primary key
435             * @throws     PortalException the company with the primary key could not be
436             *             found or if the new information was invalid or if the user
437             *             was not an administrator
438             * @deprecated As of 7.0.0, replaced by {@link #updateCompany(long, String,
439             *             String, String, boolean, byte[], String, String, String,
440             *             String, String, String, String, String, String, String,
441             *             String, java.util.List, java.util.List, java.util.List,
442             *             java.util.List, UnicodeProperties)}
443             */
444            @Deprecated
445            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
446            @Override
447            public Company updateCompany(
448                            long companyId, String virtualHost, String mx, String homeURL,
449                            String name, String legalName, String legalId, String legalType,
450                            String sicCode, String tickerSymbol, String industry, String type,
451                            String size, String languageId, String timeZoneId,
452                            List<Address> addresses, List<EmailAddress> emailAddresses,
453                            List<Phone> phones, List<Website> websites,
454                            UnicodeProperties properties)
455                    throws PortalException {
456    
457                    return updateCompany(
458                            companyId, virtualHost, mx, homeURL, name, legalName, legalId,
459                            legalType, sicCode, tickerSymbol, industry, type, size, languageId,
460                            timeZoneId, addresses, emailAddresses, phones, websites,
461                            properties);
462            }
463    
464            /**
465             * Update the company's display.
466             *
467             * @param  companyId the primary key of the company
468             * @param  languageId the ID of the company's default user's language
469             * @param  timeZoneId the ID of the company's default user's time zone
470             * @throws PortalException if the company's default user could not be found
471             *         or if the user was not an administrator
472             */
473            @Override
474            public void updateDisplay(
475                            long companyId, String languageId, String timeZoneId)
476                    throws PortalException {
477    
478                    if (!roleLocalService.hasUserRole(
479                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
480    
481                            throw new PrincipalException();
482                    }
483    
484                    companyLocalService.updateDisplay(companyId, languageId, timeZoneId);
485            }
486    
487            /**
488             * Updates the company's logo.
489             *
490             * @param  companyId the primary key of the company
491             * @param  bytes the bytes of the company's logo image
492             * @return the company with the primary key
493             * @throws PortalException if the company's logo ID could not be found or if
494             *         the logo's image was corrupted or if the user was an
495             *         administrator
496             */
497            @Override
498            public Company updateLogo(long companyId, byte[] bytes)
499                    throws PortalException {
500    
501                    if (!roleLocalService.hasUserRole(
502                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
503    
504                            throw new PrincipalException();
505                    }
506    
507                    return companyLocalService.updateLogo(companyId, bytes);
508            }
509    
510            /**
511             * Updates the company's logo.
512             *
513             * @param  companyId the primary key of the company
514             * @param  inputStream the input stream of the company's logo image
515             * @return the company with the primary key
516             * @throws PortalException if the company's logo ID could not be found or if
517             *         the logo's image was corrupted or if the user was an
518             *         administrator
519             */
520            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
521            @Override
522            public Company updateLogo(long companyId, InputStream inputStream)
523                    throws PortalException {
524    
525                    if (!roleLocalService.hasUserRole(
526                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
527    
528                            throw new PrincipalException();
529                    }
530    
531                    return companyLocalService.updateLogo(companyId, inputStream);
532            }
533    
534            /**
535             * Updates the company's preferences. The company's default properties are
536             * found in portal.properties.
537             *
538             * @param  companyId the primary key of the company
539             * @param  properties the company's properties. See {@link
540             *         com.liferay.portal.kernel.util.UnicodeProperties}
541             * @throws PortalException if the user was not an administrator
542             */
543            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
544            @Override
545            public void updatePreferences(long companyId, UnicodeProperties properties)
546                    throws PortalException {
547    
548                    if (!roleLocalService.hasUserRole(
549                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
550    
551                            throw new PrincipalException();
552                    }
553    
554                    companyLocalService.updatePreferences(companyId, properties);
555            }
556    
557            /**
558             * Updates the company's security properties.
559             *
560             * @param  companyId the primary key of the company
561             * @param  authType the company's method of authenticating users
562             * @param  autoLogin whether to allow users to select the "remember me"
563             *         feature
564             * @param  sendPassword whether to allow users to ask the company to send
565             *         their passwords
566             * @param  strangers whether to allow strangers to create accounts to
567             *         register themselves in the company
568             * @param  strangersWithMx whether to allow strangers to create accounts
569             *         with email addresses that match the company mail suffix
570             * @param  strangersVerify whether to require strangers who create accounts
571             *         to be verified via email
572             * @param  siteLogo whether to to allow site administrators to use their own
573             *         logo instead of the enterprise logo
574             * @throws PortalException if the user was not an administrator
575             */
576            @JSONWebService(mode = JSONWebServiceMode.IGNORE)
577            @Override
578            public void updateSecurity(
579                            long companyId, String authType, boolean autoLogin,
580                            boolean sendPassword, boolean strangers, boolean strangersWithMx,
581                            boolean strangersVerify, boolean siteLogo)
582                    throws PortalException {
583    
584                    if (!roleLocalService.hasUserRole(
585                                    getUserId(), companyId, RoleConstants.ADMINISTRATOR, true)) {
586    
587                            throw new PrincipalException();
588                    }
589    
590                    companyLocalService.updateSecurity(
591                            companyId, authType, autoLogin, sendPassword, strangers,
592                            strangersWithMx, strangersVerify, siteLogo);
593            }
594    
595    }