Exemple #1
0
    def Auth(self, request, *args, **kwargs):
        token = kwargs['token']
        #token = token[:len(token)-1]
        #print("=====token=====")
        #print(token)
        #print("======end======")
        #if len(token)!=64:
        #    return False
        try:
            res = requests.get("https://ip_address_removed" + token)
        except Exception as e:
            return "連線錯誤"
        root = BeautifulSoup(res.text, "lxml").select('root')
        if len(root) == 0:
            error.printe("no xml", res.text)
            return "資訊不足"
        if len(root[0].select('msg')) > 0:
            return "驗證錯誤"

        root = root[0]
        id = root.select('id')[0].text
        name = root.select('name')[0].text

        user = User.objects.filter(username=id)
        if len(user
               ) == 1 and user[0].profile.isAuth and user[0].profile.isActive:
            user = user[0]
            user.profile.fullName = name
            user.profile.save()
            try:
                self.isAdmin(root, user.profile)
            except Exception as e:
                error.printe("xml", res.text, e)
            login(request,
                  authenticate(username=user.username, password=user.username))
            messages.success(request, user.username + '帳號認證成功。')
        elif len(user) == 0:
            newUser = User()
            newUser.username = id
            newUser.set_password(id)
            newUser.save()
            profile = Profile()
            profile.user = newUser
            profile.fullName = name
            profile.type = 0
            profile.isActive = True
            profile.isAuth = True
            profile.save()
            try:
                self.isAdmin(root, profile)
            except Exception as e:
                error.printe("xml", res.text, e)
            login(request, authenticate(username=id, password=id))
            messages.success(request, newUser.username + '帳號認證成功。')
        else:
            return "登入資料錯誤"
        return ""
Exemple #2
0
def createSuperuser():
    from django.utils import timezone
    from account.models import User, Profile
    print(bcolors.OKBLUE + "\n 建立超級管理員帳號" + bcolors.ENDC)
    try:
        while(True):
            username = raw_input("帳號: ")
            check = len(list(User.objects.filter(username=username)))
            if check==0:
                break
            print(bcolors.FAIL + "帳號已經被註冊 \n" + bcolors.ENDC)
        
        password = ""
        password2 = ""
        while(True):
            password = getpass.getpass("密碼: ")
            password2 = getpass.getpass("密碼(再一次): ")
            if password==password2:
                break
            else:
                print(bcolors.FAIL + "密碼不一樣,重新輸入 \n"  + bcolors.ENDC)
                
        while(True):
            email = raw_input("電子郵件: ")
            
            if email_valid(email):
                break
            print(bcolors.FAIL + "請輸入正確的電子郵件"  + bcolors.ENDC)
        
        admin = User()
        admin.username = username
        admin.set_password(password)
        admin.email = email
        admin.is_superuser = True
        admin.is_staff = True
        admin.is_active = True
        admin.date_joined = timezone.now()
        admin.save()
        
        userProflie = Profile()
        userProflie.user = admin
        userProflie.fullName=username
        userProflie.type = 2  #0=normal user, 1=manager, 2=administrator
        userProflie.isActive = True
        userProflie.isAuth = False
        userProflie.save()
        
        print(bcolors.OKBLUE + "\n "+ username +"超級管理員帳號建立成功 \n \n" + bcolors.ENDC)
    except Exception as e:
        s = str(e)
        print(bcolors.FAIL + "\n\n取消建立帳號 \n"  + bcolors.ENDC)
        if """does not exist""" in s:
            print(bcolors.FAIL + "資料庫有問題,請檢查 \n"  + bcolors.ENDC)
Exemple #3
0
 def post(self, request, *args, **kwargs):
     data = request.POST.get("userData")
     if "," not in data:
         kwargs['error'] = '資料格式錯誤。'
     
     data = data.replace("\r\n","")
     data = data.replace("\r","")
     data = data.replace(" ","")
     temp = data.split(";")
     userArr = []
     repeat = []
     sp = ""
     
     for i in temp:
         if len(i.split(","))>2:
             kwargs['error'] = '資料格式錯誤。'
             break;
         sp = i.split(",")
         
         try:
             User.objects.get(username=sp[0])
             repeat.append(sp[0])
         except:
             userArr.append(sp)
         
     if(len(repeat)>0):
         kwargs['error'] = '資料重復:'+', '.join(repeat)
         return super(CAccountAdd3nd, self).post(request, *args, **kwargs)
     
     for i in userArr:
         newUser = User()
         newUser.username = i[0]
         newUser.set_password(i[0])
         newUser.save()
         profile = Profile()
         profile.user = newUser
         profile.fullName = i[1]
         profile.type=0
         profile.isActive=True
         profile.isAuth=True
         profile.save()    
         
         
         
     messages.success(request,'帳號加入成功。')
     return super(CAccountAdd3nd, self).post(request, *args, **kwargs)