usmannasir / cyberpanel

Cyber Panel - The hosting control panel for OpenLiteSpeed
GNU General Public License v3.0
1.61k stars 616 forks source link

Create website alias feature on API #1339

Open MarkosTech opened 1 month ago

MarkosTech commented 1 month ago

Hi, I would like the api function create website alias.

view

@csrf_exempt
def createWebsiteAlias(request):
    data = json.loads(request.body)
    adminUser = data['adminUser']
    admin = Administrator.objects.get(userName=adminUser)
    if os.path.exists(ProcessUtilities.debugPath):
        logging.writeToFile(f'Create website alias payload in API {str(data)}')

    if admin.api == 0:
        data_ret = {"existsStatus": 0, 'createParkedDomainStatus': 0,
                    'error_message': "API Access Disabled."}
        json_data = json.dumps(data_ret)
        return HttpResponse(json_data)
    wm = WebsiteManager()
    return wm.createWebsiteAliasAPI(json.loads(request.body))

websiteManager

def createWebsiteAliasAPI(self, data=None):
        try:
            adminUser = data['adminUser']
            adminPass = data['adminPass']
            adminEmail = data['ownerEmail']
            websiteOwner = data['websiteOwner']
            ownerPassword = data['ownerPassword']
            # data['ssl'] = 1
            # data['masterDomain'] = data['masterDomain']
            # aliasDomain = data['aliasDomain']
            # ssl = data['ssl']
            try :
                data['ssl'] = data['ssl']
            except:
                data['ssl'] = 1
            try:
                websitesLimit = data['websitesLimit']
            except:
                websitesLimit = 1
            try:
                apiACL = data['acl']
            except:
                apiACL = 'user'
            admin = Administrator.objects.get(userName=adminUser)
            if hashPassword.check_password(admin.password, adminPass):
                if adminEmail is None:
                    data['adminEmail'] = "example@example.org"
                try:
                    acl = ACL.objects.get(name=apiACL)
                    websiteOwn = Administrator(userName=websiteOwner,
                                               password=hashPassword.hash_password(ownerPassword),
                                               email=adminEmail, type=3, owner=admin.pk,
                                               initWebsitesLimit=websitesLimit, acl=acl, api=1)
                    websiteOwn.save()
                except BaseException:
                    pass
            else:
                data_ret = {"existsStatus": 0, 'createWebSiteAliasStatus': 0, 'error_message': "Could not authorize access to API"}
                json_data = json.dumps(data_ret)
                return HttpResponse(json_data)
            return self.submitAliasCreation(admin.pk, data)
        except BaseException as msg:
            data_ret = {'createWebSiteAliasStatus': 0, 'error_message': str(msg), "existsStatus": 0}
            json_data = json.dumps(data_ret)
            return HttpResponse(json_data)

Thanks and hope the feature can be added soon upstream.