<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[מניעת מופע כפול של מחלקה]]></title><description><![CDATA[<p dir="rtl">יש לי פרוייקט ב-PHP שבו יש מחלקה שמטפלת בבקשות API ומאפשרת גישה למסד הנתונים רק אם התקיימו אחד משני התנאים: אימות recaptcha או שקיים session פעיל (המשתמש כבר מחובר, משמע שכבר עבר אימות)<br />
הבעיה שלי היא שבמקרה שבו האימות מתבצע דרך recaptcha אזי ברור שאני יצור מופע חדש של DB לאחר שלב האימות<br />
אך במקרה שהמשתמש כבר מחובר זה אומר שה-class שיצר את מופע האובייקט של המשתמש כבר ניגש ל-DB ויצר כבר מופע אחד של המחלקה<br />
מצד אחד לא הגיוני לגשת ל-DB דרך <code>this-&gt;user-&gt;db</code>, אך גם לא מסתבר ליצור מופע נפרד של DB עבור ה-user ושני עבור ה-API<br />
מה הפתרון?</p>
]]></description><link>https://tchumim.com/topic/14444/מניעת-מופע-כפול-של-מחלקה</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 23:08:09 GMT</lastBuildDate><atom:link href="https://tchumim.com/topic/14444.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 04 Jan 2023 11:29:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to מניעת מופע כפול של מחלקה on Thu, 05 Jan 2023 21:22:00 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dovid" aria-label="Profile: dovid">@<bdi>dovid</bdi></a> כתב ב<a href="/post/148360">מניעת מופע כפול של מחלקה</a>:</p>
<blockquote>
<p dir="rtl">לממש DI בPHP</p>
</blockquote>
<p dir="rtl">בסופו של דבר העברתי את שלב אימות ה-recaptcha אל המחלקה שאחראית על יצירת ה-user, וכך אני יוצר מופע של user, בודק האם השאילתה הנוכחית מאומתת עם $_SESSION או recaptcha, ואם אחד משתי התנאים האלו נכון - יוצר מופע של db ומעביר אותו עם setter injection ל-user</p>
]]></description><link>https://tchumim.com/post/148399</link><guid isPermaLink="true">https://tchumim.com/post/148399</guid><dc:creator><![CDATA[איש נחמד]]></dc:creator><pubDate>Thu, 05 Jan 2023 21:22:00 GMT</pubDate></item><item><title><![CDATA[Reply to מניעת מופע כפול של מחלקה on Wed, 04 Jan 2023 20:30:48 GMT]]></title><description><![CDATA[<p dir="rtl"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D7%90%D7%99%D7%A9-%D7%A0%D7%97%D7%9E%D7%93" aria-label="Profile: איש-נחמד">@<bdi>איש-נחמד</bdi></a> כל הלינקים שלך מסבירים על חסרונות הסינגלטון,<br />
אבל אף אחד מהם לא שולל את השימוש בו.<br />
לכל דבר בעולם יש חיסרון, החסרונות המדוברים לא נוגעים למקרה שלך.<br />
(אם אתה כן רוצה להיות מושלם תצטרך ללמוד לממש DI בPHP, אני לא יודע אפילו לתת לך לינקים, אולי זה <a href="https://medium.com/tech-tajawal/dependency-injection-di-container-in-php-a7e5d309ccc6" target="_blank" rel="noopener noreferrer nofollow ugc">https://medium.com/tech-tajawal/dependency-injection-di-container-in-php-a7e5d309ccc6</a>).</p>
]]></description><link>https://tchumim.com/post/148360</link><guid isPermaLink="true">https://tchumim.com/post/148360</guid><dc:creator><![CDATA[dovid]]></dc:creator><pubDate>Wed, 04 Jan 2023 20:30:48 GMT</pubDate></item><item><title><![CDATA[Reply to מניעת מופע כפול של מחלקה on Wed, 04 Jan 2023 19:59:07 GMT]]></title><description><![CDATA[<p dir="rtl">עוד על יתרונות וחסרונות הסינגלטון במאמר בעברית:<br />
<a href="https://codetime.co.il/singleton/" target="_blank" rel="noopener noreferrer nofollow ugc">https://codetime.co.il/singleton/</a></p>
]]></description><link>https://tchumim.com/post/148359</link><guid isPermaLink="true">https://tchumim.com/post/148359</guid><dc:creator><![CDATA[איש נחמד]]></dc:creator><pubDate>Wed, 04 Jan 2023 19:59:07 GMT</pubDate></item><item><title><![CDATA[Reply to מניעת מופע כפול של מחלקה on Wed, 04 Jan 2023 19:34:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dovid" aria-label="Profile: dovid">@<bdi>dovid</bdi></a> אני מעריך את התגובה המפורטת שלך</p>
<p dir="rtl">להלן מספר קישורים ב-stackoverflow<br />
<a href="https://stackoverflow.com/a/138012" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/a/138012</a><br />
<a href="https://stackoverflow.com/a/1020384" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/a/1020384</a><br />
שם תמצא עוד קישורים למכביר</p>
]]></description><link>https://tchumim.com/post/148355</link><guid isPermaLink="true">https://tchumim.com/post/148355</guid><dc:creator><![CDATA[איש נחמד]]></dc:creator><pubDate>Wed, 04 Jan 2023 19:34:04 GMT</pubDate></item><item><title><![CDATA[Reply to מניעת מופע כפול של מחלקה on Wed, 04 Jan 2023 18:16:24 GMT]]></title><description><![CDATA[<p dir="rtl"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D7%90%D7%99%D7%A9-%D7%A0%D7%97%D7%9E%D7%93" aria-label="Profile: איש-נחמד">@<bdi>איש-נחמד</bdi></a> אם אני עוקב טוב אחריך במקרה שיש כבר חיבור ישנם שתי בדיקות במהלך מחזור החיים של אותה בקשה, אחת בהתחלה ממש כדי לבדוק הרשאות של משתמש והשניה כדי להביא מידע. אתה מנסה לחסוך ליצור שני מופעי גישה למסד נתונים.<br />
אם אני צודק, אז ראשית כל <a class="plugin-mentions-user plugin-mentions-a" href="/user/shloimy" aria-label="Profile: Shloimy">@<bdi>Shloimy</bdi></a> צודק וזה כיון הפתרון, זה לא חייב להיות מבנה סינגלטון ממש, די בכך שיהיה מקום משותף לגישה לDB שנטען פעם בודדת בכל טכניקה שתבחר.<br />
אם יוצאים נגד זה בstackoverflow נסה לצרף קישור כדי שאבין מה מדובר (אולי יש בעיה עם זה בPHP).<br />
שנית, אל תתייגע לחסוך יצירת מופע לDB, זה לא משהו יקר כל כך להשקיע באופטימיזציה כל עוד לא ראית שהעסק מתחיל להיות מורגש.</p>
<p dir="rtl">בקשר למשפט הזה:</p>
<blockquote>
<p dir="rtl">הפונקציה role במחלקה User יכולה אמנם לטעון את ההרשאה מתוך ה-session (ולחסוך את הקריאה הנוספת למסד הנתונים, שתיצור מופע נוסף של החיבור ל-DB), אבל האם זה מספיק מאובטח?</p>
</blockquote>
<p dir="rtl">אם כוונתך במילה session ל$_SESSION של PHP, אז אני לא רואה כל בעיית אבטחה לאחסן בו מידע (אם אתה חושב על בעיה תכתוב אולי לא נתתי על זה את הדעת), רק צריך לא לשים שמה מידי הרבה כי זה תופס פיזית מקום בדיסק, ובשעת השאילתה זה גם לוקח מקום בRAM.</p>
]]></description><link>https://tchumim.com/post/148353</link><guid isPermaLink="true">https://tchumim.com/post/148353</guid><dc:creator><![CDATA[dovid]]></dc:creator><pubDate>Wed, 04 Jan 2023 18:16:24 GMT</pubDate></item><item><title><![CDATA[Reply to מניעת מופע כפול של מחלקה on Wed, 04 Jan 2023 12:38:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dovid" aria-label="Profile: dovid">@<bdi>dovid</bdi></a> כתב ב<a href="/post/148345">מניעת מופע כפול של מחלקה</a>:</p>
<blockquote>
<p dir="rtl">מה זה אומר מופע של DB?</p>
</blockquote>
<p dir="rtl">חיבור למסד הנתונים</p>
<p dir="rtl">המחלקה של user צריכה להחזיר מידע על המשתמש מתוך מסד הנתונים<br />
המחלקה של ה-API צריכה להחזיר מידע ממסד הנתונים וגם לכתוב אליו, אבל רק לאחר אימות גישה באמצעות בדיקה אם קיים session פעיל או אימות recaptcha (בצד שרת)<br />
המחלקה של ה-API גם תרצה לבדוק את הרשאות המשתמש (משתמש, נציג, או מנהל) כדי לנהל את הגישה לפונקציות השונות</p>
<p dir="rtl">אלו 2 הפונקציות שמאמתות את הגישה לפונקציה במחלקה API:</p>
<pre><code>private function check_permissions( User $user, string $action )
    {
        return isset( $this-&gt;allowed_actions_array[$action]['permissions'] ) &amp;&amp; in_array( $this-&gt;user-&gt;role(), $this-&gt;allowed_actions_array[$action]['permissions'] );
    }

    private function access_auth( User $user, string $action )
    {
        if( !isset( $this-&gt;allowed_actions_array[$action]['auth'] ) ) $this-&gt;return_json( false, 'אירעה שגיאה בהגדרות הפעולה' );

        $auth = $this-&gt;allowed_actions_array[$action]['auth'];
        switch ( $auth ) {
            case 'logged_in':
                return $user-&gt;is_logged_in();
                break;
            case '':
                if( !isset( $_GET['r'] ) ) return false;
                else return $this-&gt;recaptcha_verification( $_GET['r'] );
                break;
            default:
                return false;
                break;
        }
    }
</code></pre>
<p dir="rtl">הפונקציה <code>role</code> במחלקה <code>User</code> יכולה אמנם לטעון את ההרשאה מתוך ה-session (ולחסוך את הקריאה הנוספת למסד הנתונים, שתיצור מופע נוסף של החיבור ל-DB), אבל האם זה מספיק מאובטח?</p>
]]></description><link>https://tchumim.com/post/148346</link><guid isPermaLink="true">https://tchumim.com/post/148346</guid><dc:creator><![CDATA[איש נחמד]]></dc:creator><pubDate>Wed, 04 Jan 2023 12:38:59 GMT</pubDate></item><item><title><![CDATA[Reply to מניעת מופע כפול של מחלקה on Wed, 04 Jan 2023 12:30:06 GMT]]></title><description><![CDATA[<p dir="rtl"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D7%90%D7%99%D7%A9-%D7%A0%D7%97%D7%9E%D7%93" aria-label="Profile: איש-נחמד">@<bdi>איש-נחמד</bdi></a><br />
לא מספיק ברור לי איך אתה עובד, לא הבנתי טוב את המונחים, למשל:</p>
<blockquote>
<p dir="rtl">שאני יצור מופע חדש של DB</p>
</blockquote>
<p dir="rtl">מה זה אומר מופע של DB?</p>
<p dir="rtl">בקשר לסינגלטון ש@Shloimy הציע זה לא רלוונטי פה כי זה מופע יחיד רק במחזור החיים של אותו request (אחד החסרונות של PHP).</p>
]]></description><link>https://tchumim.com/post/148345</link><guid isPermaLink="true">https://tchumim.com/post/148345</guid><dc:creator><![CDATA[dovid]]></dc:creator><pubDate>Wed, 04 Jan 2023 12:30:06 GMT</pubDate></item><item><title><![CDATA[Reply to מניעת מופע כפול של מחלקה on Wed, 04 Jan 2023 12:29:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/shloimy" aria-label="Profile: Shloimy">@<bdi>Shloimy</bdi></a> כתב ב<a href="/post/148343">מניעת מופע כפול של מחלקה</a>:</p>
<blockquote>
<p dir="rtl"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D7%90%D7%99%D7%A9-%D7%A0%D7%97%D7%9E%D7%93" aria-label="Profile: איש-נחמד">@<bdi>איש-נחמד</bdi></a> לדעתי, <a href="https://en.wikipedia.org/wiki/Singleton_pattern" target="_blank" rel="noopener noreferrer nofollow ugc">singleton</a>. לא מתעסק עם PHP אבל <a href="https://refactoring.guru/design-patterns/singleton/php/example" target="_blank" rel="noopener noreferrer nofollow ugc">זה</a> דוגמא אחת למימוש בשפה הנ"ל.</p>
</blockquote>
<p dir="rtl">יש דרך אחרת? קראתי מאוד הרבה חומר נגד המבנה הזה, כל מי שמעז להציע את זה ב-stackoverflow מקבל מיד ביקורת שלילית</p>
]]></description><link>https://tchumim.com/post/148344</link><guid isPermaLink="true">https://tchumim.com/post/148344</guid><dc:creator><![CDATA[איש נחמד]]></dc:creator><pubDate>Wed, 04 Jan 2023 12:29:02 GMT</pubDate></item><item><title><![CDATA[Reply to מניעת מופע כפול של מחלקה on Wed, 04 Jan 2023 12:20:50 GMT]]></title><description><![CDATA[<p dir="rtl"><a class="plugin-mentions-user plugin-mentions-a" href="/user/%D7%90%D7%99%D7%A9-%D7%A0%D7%97%D7%9E%D7%93" aria-label="Profile: איש-נחמד">@<bdi>איש-נחמד</bdi></a> לדעתי, <a href="https://en.wikipedia.org/wiki/Singleton_pattern" target="_blank" rel="noopener noreferrer nofollow ugc">singleton</a>. לא מתעסק עם PHP אבל <a href="https://refactoring.guru/design-patterns/singleton/php/example" target="_blank" rel="noopener noreferrer nofollow ugc">זה</a> דוגמא אחת למימוש בשפה הנ"ל.</p>
]]></description><link>https://tchumim.com/post/148343</link><guid isPermaLink="true">https://tchumim.com/post/148343</guid><dc:creator><![CDATA[Shloimy]]></dc:creator><pubDate>Wed, 04 Jan 2023 12:20:50 GMT</pubDate></item></channel></rss>