วันศุกร์ที่ 19 เมษายน พ.ศ. 2567

วิธีสร้าง API และ JSON สำหรับย้ายข้อมูล

  Section 1: สร้าง Google Service Account


  1. เข้าสู่ระบบ Google Workspace ด้วยสิทธิ์ Super Admin
  2. เปิด Tab ใหม่ และเข้าสู่หน้า Service Accounts
  3. เลือกปุ่ม CREATE PROJECT เพื่อสร้าง Project ขึ้นมา

  4. ใส่ชื่อ Project name และเลือก Organization ให้ถูกต้อง และกด CREATE

  5. จากนั้นระบบจะเข้าสู่หน้า Project ที่เราสร้างขึ้น ให้เลือก + CREATE SERVICE ACCOUNT เพื่อสร้าง Service account

  6. ใส่รายละเอียด Service account name และ Service account ID จากนั้นกด CREATE AND CONTINUE และเลือก DONE

  7. จากนั้นระบบจะทำการสร้าง Service account ได้ตามรูป

  8. เลือกเข้าไปยัง Service account ดังกล่าว และคัดลอก Unique ID ไว้ จากนั้นไปที่ Tab KEYS ด้านบน

  9. เมื่อเข้าสู่หน้า KEYS ทำการเลือก ADD KEY > Create new key และเลือก JSON เพื่อสร้าง Key เพื่อใช้สำหรับการย้ายข้อมูล
    จากนั้นจะได้ไฟล์นามสกุล .json เพื่อใช้สำหรับขั้นตอนในอนาคตถัดไป


Section 2: เปิดใช้งาน API เพื่อใช้งานสำหรับ Project ที่สร้างขึ้น

  1. เข้าสู่หน้า  Developer page for API Library เพื่อเปิดสิทธิ์การเข้าถึง Service ต่างๆ
  2. เมื่อเข้าสู่หน้า API Library ในกรณีที่มีหลาย Project ใตรวจสอบ Project ให้ถูกต้อง ให้เป็น Project ที่สร้างขึ้นใน Section ที่ 1

  3. ให้ค้นหา Service ต่อไปนี้ และทำการ Enable ให้ครบทุกๆ Service
    • Gmail API
    • Google Calendar API
    • Contacts API
    • Google People API


Section 3: ให้สิทธิ์ Service account เข้าถึง Google Tenant
  1. เข้าสู่หน้า  Google Workspace Admin page
  2. เลือกเมนู Security > API controls > Domain wide delegation > MANAGE DOMAIN WIDE DELEGATION

  3. เลือก Add new และใส่ Client ID ซึ่งเป็นค่า Unique ID ที่ได้จากขั้นตอนการสร้าง Service account
    หลังจากนั้นให้ใส่ค่าด้านล่างโดย Copy วางทั้งหมดได้เลยในช่อง OAuth scopes
    https://mail.google.com/,https://www.googleapis.com/auth/calendar,https://www.google.com/m8/feeds/,https://www.googleapis.com/auth/gmail.settings.sharing,https://www.googleapis.com/auth/contacts

วันพุธที่ 21 กุมภาพันธ์ พ.ศ. 2567

วิธีตั้งค่า CRONJOB ใน DA สำหรับเช็ค Force change pass

 - ช่อง command ใช้คำสั่ง

php /home/admin/domains/panel.technologyland.co.th/public_html/testfunction/cronfcp.php

 - ช่อง minute ใส่ 0

- ช่อง Hours ใส่ 1 = 08.00 , 6 = 13.00

วิธีเพิ่ม code force change password สำหรับ WEBMAIL

 สร้างไฟล์ condb.php เพื่อเชื่อม DB


ไฟล์ที่ 1 /var/www/html/index.php

- เพิ่ม code บรรทัด ที่ 48 ในไฟล์ index.php

include('condb.php');

- เพิ่มฟังก์ชั่น getRandomStringShuffle (ใช้สร้าง token) กับ disableuser (ใช้ disable user)

function getRandomStringShuffle($length = 50)
{
    $stringSpace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $stringLength = strlen($stringSpace);
    $string = str_repeat($stringSpace, ceil($length / $stringLength));
    $shuffledString = str_shuffle($string);
    $randomString = substr($shuffledString, 1, $length);
    return $randomString;
}


function disableuser($user){
    $email = [];
    array_push($email,$user);
    $data = json_encode($email);

    $curl = curl_init("https://panel.technologyland.co.th/api/alertdisable.php");
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $headers[] = 'Content-Type: application/*+json';
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($curl);
    $resultStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);

    
}


- เพิ่ม code ทั้งหมด ไว้หลัง code ในภาพ


 

        $key_session = $obj->getRandomStringShuffle(); // เรียกใช้ฟังก์ชั่น สร้าง Token
        $exp_key_session = date("Y-m-d H:i:s",strtotime("+5 minutes")); // กำหนดเวลาหมดอายุ 5 นาที
       

        // เช็คสถานะ force change ใน db
        $sql_q = "SELECT token,key_session,expire_key_session,status,policy_pass FROM `tb218_force_change_password` WHERE user_name like '".$_SESSION['username']."' AND policy_pass = '1001' LIMIT 1";
        $a = mysqli_query( $connect, $sql_q );
        $result =mysqli_fetch_array($a,MYSQLI_ASSOC);

        // สร้าง url เพื่อใช้สำหรับ redirect ในหน้า force change password
        $url = 'http' . (($_SERVER['HTTPS'] == 'on') ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

        // เช็คว่ามี สถานะ force change หรือไม่ ถ้ามีก้อเข้าเงื่อนไข
        if($a->num_rows > 0 && $result['status'] == 'no'){
            $u = array("token"=>$result['token'],"key_session"=>$key_session,"policy"=>$result['policy_pass'],"url" => $url);
            $urlArray = urlencode(base64_encode(json_encode($u)));
            $sql_u = "UPDATE `tb218_force_change_password` SET `key_session` = '$key_session',expire_key_session = '$exp_key_session',session_expired = 'no' WHERE user_name like '".$_SESSION['username']."' AND policy_pass = '1001'";
            $b = mysqli_query( $connect, $sql_u );
            $disables = $obj->disableuser($_SESSION['username']);
            $RCMAIL->logout_actions();
            $RCMAIL->kill_session();
            
            header('Location: https://panel.technologyland.co.th/forcechangepass/fchp.php?u='.$urlArray);
            exit();
        } else {
           
            $sql_q = "SELECT token,key_session,expire_key_session,status,policy_pass,date_reset_password FROM `tb218_force_change_password` WHERE user_name like '".$_SESSION['username']."' AND policy_pass != '1001' LIMIT 1";
            $a = mysqli_query( $connect, $sql_q );
            $result =mysqli_fetch_array($a,MYSQLI_ASSOC);
            if($a->num_rows > 0){
                $reset_date = strtotime(date($result['date_reset_password']));
                $date_now = strtotime(date('Y-m-d'));
                if($date_now >= $reset_date  ){
                    $u = array("token"=>$result['token'],"key_session"=>$key_session,"policy"=>$result['policy_pass'],"time" => date("Y-m-d H:i:s"),"username" => $_SESSION['username'],"url" => $url);
                    $urlArray = urlencode(base64_encode(json_encode($u)));
                    $sql_u = "UPDATE `tb218_force_change_password` SET `key_session` = '$key_session',expire_key_session = '$exp_key_session',session_expired = 'no' WHERE user_name like '".$_SESSION['username']."' AND policy_pass = '".$result['policy_pass']."'";
                    $b = mysqli_query( $connect, $sql_u );
                    $disables = $obj->disableuser($_SESSION['username']);
                    $RCMAIL->logout_actions();
                    $RCMAIL->kill_session();
                    
                    header('Location: https://panel.technologyland.co.th/forcechangepass/fchp.php?u='.$urlArray);
                    exit();
                }
            }
        }

======================================================================

ไฟล์ที่ 2 /var/www/html/skins/larry/templates/login.html

เพิ่มcode ด่านลงในปุ่ม log in 

oncliick="clicklogin()"

เพิ่ม code ด้านล่างใน <form>

id="form-login"

 เพิ่ม script ด้านล่างสุดของ ไฟล์

<script>
function clicklogin(){
  var user = document.getElementById("rcmloginuser").value;
  var pass = document.getElementById("rcmloginpwd").value;
  //           form_login.submit();
  // console.log(val);
 
  const payload_jwt_data_checkpay = {
                user:user,
                pass: pass
            };
     
  // console.log(val);
  const options = {
        method: 'POST',
        headers: {accept: 'application/json', 'content-type': 'application/*+json'},
        body: JSON.stringify(payload_jwt_data_checkpay)
        };
        fetch('https://panel.technologyland.co.th/api/alertactive.php', options)
        .then(response => response.json())
          .then(data => {
        // console.log(data); // แสดงข้อมูล JSON จาก then ข้างบน
       
        var form_login = document.getElementById("form-login");
            form_login.submit();
        
        // document.getElementById("test_checkpay").value = data.payload;
        // console.log(parseJwt(data.payload));
        
        })
        
    
  }

</script>